【发布时间】:2017-12-09 21:44:38
【问题描述】:
我尝试了以下代码,但它卡在 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");
}
【问题讨论】:
-
什么意思“卡住了”?它正在等待您输入一行文本,并在您按 Enter 时继续(因为您正在阅读来自
System.in)。 -
您使用什么代码来运行在控制台中产生 1000 多行数据的进程(如果这确实是您正在做的)?我相信您真正想要的可能是:
BufferedReader in = new BufferedReader(new InputStreamReader(myProcess.getInputStream()));而不是:System.in。当然,这涉及到更多的代码,但如果这是你想要做的,那么可以提供这些信息。
标签: java jakarta-ee