Friday, 29 August 2014

To Reading data from given URL by using URL class and openStream() then Write in file

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class OpenStr {

    public int add(int a,int b)
    {
        return(a+b);
    }
   
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException {
        String str="";
        BufferedWriter out=new BufferedWriter(new FileWriter("google.html"));
        // TODO Auto-generated method stub
try{
   
        URL u=new URL("http://www.w3schools.com/");
        DataInputStream in=new DataInputStream(u.openStream());
        while(in.readLine()!=null)
        {
           
            //System.out.println(in.readLine());
            str+=in.readLine();
           
        }
        in.close();
}catch (Exception e) {
    // TODO: handle exception
}   
       
        System.out.println(str);
        out.write(str);
        out.close();
    }

}

No comments:

Post a Comment