我尝试了以下代码,但它被卡在String lineFromInput = in.readLine();如果没有这一行,则txt文件为空。
我使用的代码是:
try
{
File file = new File("out.txt"); //Your file
FileOutputStream fos = new FileOutputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String lineFromInput = in.readLine();
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
ps.println(lineFromInput);
// System.out.println("This goes to out.txt");
}
catch(IOException e)
{
System.out.println("Error during reading/writing");
}
答案 0 :(得分:0)
正如Andreas所说,它等待标准输入写入文件。 你应该真的使用log4j来达到这个目的。
有关详细信息,请参阅how to make log4j to write to the console as well。