-3

How can I make the below code, save the webpage it self to txt file, I don't need the code of the webpage I need the webpage itself to be saved as text, like when we press CTRL + S and choose save as txt. As well, how to make the browse hidden. Thank you in advance :)

import java.awt.Desktop;
import java.io.File;
import java.net.URI;

public class Main {

public static void main(String[] args) throws Exception {

    Desktop d = Desktop.getDesktop();
    String url = "http://w3-01.ibm.com/pc/entitle/pg2/Service.wss/mts/Lookup?type=8205&serial=06202ET";
    d.browse(new URI(url));
}
}
  • " I don't need the code of the webpage I need the webpage itself" - a web page is a bunch of HTML, with possibly some JavaScript or other content. It's "code". If you want to extract some data from it, you'll need to parse it. Search for that, it's a frequent question. – Mat Mar 07 '16 at 16:24

1 Answers1

-1

Here is a working example in Java 8:

import java.io.*;
import java.net.URL;

public class Main {

    public static void main(String[] args) throws IOException {
        URL url = new URL("https://www.google.com/");
        String file = System.getProperty("java.io.tmpdir") + "google.txt";
        System.out.println("Saving file to " + file);
        try (InputStream in = url.openStream();
            OutputStream os = new FileOutputStream(file)) {
            int b;
            while ((b = in.read()) != -1) {
                os.write(b);
            }
        }
    }
}
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • This always gives me this error Exception in thread "main" java.io.FileNotFoundException: \tmp\google.txt (The system cannot find the path specified) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.(FileOutputStream.java:213) at java.io.FileOutputStream.(FileOutputStream.java:101) at Main1.main(Main1.java:12) – Aboelmagd Saad Aboelmagd Mar 07 '16 at 16:24
  • Change `String file = "/tmp/google.txt";` to a path that is writeable on your system. – Asaph Mar 07 '16 at 16:25
  • If you're using windows `/tmp/google.txt` won't work. Change it to a valid windows-compatible path. – Asaph Mar 07 '16 at 16:27
  • NoW IT SAYS Exception in thread "main" java.io.FileNotFoundException: \temp (Access is denied) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.(FileOutputStream.java:213) at java.io.FileOutputStream.(FileOutputStream.java:101) at Main1.main(Main1.java:12) – Aboelmagd Saad Aboelmagd Mar 07 '16 at 16:28
  • Looks like your user does not have access to write to `\temp`? Should it be instead `C:\temp`? Try a path you know your user can write to, such as your desktop. – Asaph Mar 07 '16 at 16:30
  • Also, don't forget to include the filename (`google.txt` in my example code) in the path too. – Asaph Mar 07 '16 at 16:32
  • Would the downvoter care to comment? – Asaph Mar 07 '16 at 16:41
  • You can also try `String file = System.getProperty("java.io.tmpdir") + "google.txt";` which should always succeed (unless your disk is full). – Asaph Mar 07 '16 at 16:53
  • 1
    Thank you so much, it works, but it is not what I want, I don't want the code of the page to be saved as text, I want to save the page itself as text, like when you do CTRL + S and choose save as txt. Can this be done please? – Aboelmagd Saad Aboelmagd Mar 08 '16 at 21:09
  • So you want the javascript run which might impact the DOM? For that you will need to use something like HTMLUnit or a headless Chrome browser. – Asaph Mar 08 '16 at 21:12
  • Hello I have created a small program that can scan txt files. I want to know how to make the below if statment, print the next line if the word Small was find in the line. int lineNumber = 1; while(scnr.hasNextLine()) { String line = scnr.nextLine(); if (line.contains("small")) { System.out.println(line); } – Aboelmagd Saad Aboelmagd Mar 10 '16 at 11:07