【发布时间】:2023-03-16 03:17:02
【问题描述】:
我尝试使用此代码建议 (http://www.daniweb.com/forums/thread23883.html#) 将控制台输出写入 txt 文件,但没有成功。怎么了?
try {
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in.readLine();
//create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//output to the file a line
out.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e1) {
System.out.println("Error during reading/writing");
}
【问题讨论】:
-
您提供的代码示例将控制台 input 写入文件。目前还不是很清楚你想要达到什么目的。你能提供更多细节吗?
-
我在控制台上有许多由 system.out.println 产生的输出。我正在尝试将所有这些输出写入 .txt 文件。