简体   繁体   中英

How can I edit the code from url and save to .html file on desktop

Teacher ask me to write java to read html file from school web and cut all unneeded parts (everything that don't need in that web page), left only the announcement part at the center of the website and save as another html file.

I now can read html file into java now but can't write code to edit (to cut unneeded) and save as html file.

code that has been done so far is:

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

public class Html {

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

            URL chula = new URL("http://www.ise.eng.chula.ac.th");
            URLConnection yc = chula.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc
                    .getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
        } 
    }
}

change System.out.println(inputLine); to:

    PrintWriter output = new PrintWriter("newFile.html");
    output.println(inputLine);

This will create a new file with all the contents of the inputLine typed into the new file.

I have edited the code you put, and think i found the answer you needed

What you have to do, is use a scanner, with the InputStreamReader. The scanner will read the file, or in this case, the URL that you are using. You then must create a new File using the PrintWriter class, and change the while loop to this:

    while(in.hasNext()) {}

This will read URL that you want, and it will go through each line of the file(URL) and will not stop until it reaches the end. Then you must create a String holding the information from the URl within the while loop. The last thing to do is just write the contents into the file, and ALWAYS make sure to close BOTH the scanner, and the file that you are writing to.

Here is the code:

    import java.io.*;
    import java.net.*;
    import java.util.*;

public class Html {
    public static void main(String[] args) throws IOException {

        URL chula = new URL("http://www.ise.eng.chula.ac.th");
        URLConnection yc = chula.openConnection();
        //BufferedReader in = new BufferedReader(new InputStreamReader(yc
          //      .getInputStream()));
        Scanner in = new Scanner(new InputStreamReader(yc.getInputStream()));

        PrintWriter output = new PrintWriter("newFile.html");
        while (in.hasNext()) {
            String inputLine = in.nextLine();
            output.println(inputLine);
        }
        in.close();
        output.close();
    } 
}

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM