我用java编写的代码正在抓取网站的索引页面,但是当我尝试保存文件时,它会保存一个空白文档。
这是代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JOptionPane;
import java.io.*;
public class source {
public static void main(String[] args) throws IOException {
String filename;
filename = JOptionPane.showInputDialog("Enter the site");
PrintWriter outputFile = new PrintWriter("files\\"+filename+".html");
URL url = new URL("http://"+filename);
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
outputFile.println(line);
}
System.exit(0);
}
}
我究竟做错了什么?
我需要添加什么来保存 html 吗?