0

如何将控制台消息从 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");
       }     
  }
4

2 回答 2

3

你需要改变 FileOutputStream("C:/Error.txt")

FileOutputStream("C:\\Error.txt")

正如 Juned Ahsan 所说。此外,如果您需要将从控制台获取的文本写入

Error.txt

你需要添加一行

System.out.println(lineFromInput);

System.setOut(out);

行,您可以在其中设置默认输出位置。

于 2013-06-12T06:39:20.390 回答
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
                }
            }
        }
于 2013-06-12T08:29:25.497 回答