以下代码将页面内容保存到文件中:
import java.net.*;
import java.io.*;
public class url
{
public static void main(String[] args)
{
try
{
URL PageUrl;
URLConnection GetConn = null;
GetConn = null;
PageUrl = new URL("https://www.google.ru/");
GetConn = PageUrl.openConnection();
GetConn.connect();
InputStreamReader ReadIn = new InputStreamReader(GetConn.getInputStream());
BufferedReader BufData = new BufferedReader(ReadIn);
String htmlFileName = ("C:\\hello.html");
FileWriter FWriter = new FileWriter(htmlFileName);
BufferedWriter BWriter = new BufferedWriter(FWriter);
String UrlData = null;
while ((UrlData = BufData.readLine()) != null)
{
BWriter.write(UrlData);
BWriter.newLine();
}
BWriter.close();
}
catch(IOException io)
{
System.out.println(io);
}
}
}
但是我需要文件与网站的页面具有相同的名称,例如,它必须以某种方式获取网页的名称并将其分配为文件的名称。