我需要你的帮助。我创建了下面的代码,它将某些网站的内容存储在本地硬盘上。top_urls.txt 包含大量网站。
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader( new FileReader("top_urls.txt") );
try {
String content = "contents_urls";
int i=1;
Scanner freader = new Scanner(input);
while ( freader.hasNextLine() ) {
String url = freader.nextLine();
System.out.println(url);
String out = new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next();
System.out.println(out);
String file_name = content +i;
PrintWriter pwout = new PrintWriter( new FileWriter ("site_"+ i+ ".txt",true) );
pwout.println(out);
i++;
}
freader.close();
} catch (IOException ex) {
ex.printStackTrace();
} System.out.println("Done");
}
所以,当一个网站的内容存储文件被命名为site_1.txt、site_2.txt等。我想用网站名称存储内容文件,例如。www.google.com.txt 等
我试过了
PrintWriter pwout = new PrintWriter( new FileWriter (url+".txt",true) );
,但没有用。任何想法如何做到这一点?提前致谢