如何将Eclipse中的控制台消息写入文件?
请检讨一下,如果有人给出解决方案,我将不胜感激。 我的示例程序如下所示
这是控制台消息:
Invalid Excel Path Specified "+path
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String lineFromInput = in.readLine();
PrintStream out = new PrintStream(new FileOutputStream("C:/Error.txt"));
System.setOut(out);
out.close();
}
catch(IOException e1) {
System.out.println("Error during reading/writing");
}
}
答案 0 :(得分:3)
你需要改变
FileOutputStream("C:/Error.txt")
到
FileOutputStream("C:\\Error.txt")
Error.txt
你需要添加像
这样的行 之后 System.out.println(lineFromInput);
System.setOut(out);
行,您可以在其中设置默认输出位置。
答案 1 :(得分:0)
PrintStream out = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
String lineFromInput = in.readLine();
out = new PrintStream(new FileOutputStream("C:/Error.txt"));
System.setOut(out);
System.out.println(lineFromInput);
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("Error during reading/writing");
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
//ignore
}
}
}