Thursday, 23 August 2012

Read page source code ( Java )

 Read page source code and save in text file

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;


public class Network {
    public static void main(String[] args) throws IOException {
        Writer output = null;
        boolean flag = true;
        File file = new File("test.txt"); // your file
        output = new BufferedWriter(new FileWriter(file));
        try {
                URL web = new URL("http://youhost.com"); // choice web
                BufferedReader in = new BufferedReader( new InputStreamReader(web.openStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null)
                        output.write(inputLine + '\n');
                in.close();         
            output.close();
        } catch (MalformedURLException me) {
            System.out.println(me);
   
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

No comments:

Post a Comment