【问题标题】:How do i put my 1000+ line console data in a txt file?如何将我的 1000+ 行控制台数据放入 txt 文件中?
【发布时间】: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


【解决方案1】:

正如 Andreas 所说,它等待标准输入写入文件。 你真的应该为此目的使用 log4j。

更多详情请见how to make log4j to write to the console as well

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多