我需要有关 IO java 代码的帮助,我正在尝试将 java 控制台输出 [来自 URL 类] 保存到本地机器中的文件中,我能够获得输出需要帮助将该文件保存到本地机器
为了得到输出我做了这个编码
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.yahoo.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
注意:我有另一个代码,我可以在其中将输出保存到文件中,但无法将两者关联起来,我尝试过扩展,但没有进展,这是那个类
import java.io.*;
class FileWrite {
public static void main(String args[]) {
try{
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}