java 实验五保存网页到本地

实验要求:

 编写一个程序,实现访问一个网址吧并将指定的一个页面保存到本地。、

package 实验五保存网站;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class testMain {
    public static void main(String argv[]) 
    {
        try
        {
            URL url=null;
            url=new URL("https://www.baidu.com/");
            BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream()));
            File infile =new File("file.html");
            FileWriter fin=new FileWriter(infile);
            String line;
            while((line=in.readLine())!=null)
            {
                fin.write(line);
            }
            fin.close();
            in.close();
        }catch (MalformedURLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/lhclqslove/p/9175037.html