2013-06-12 34 views
0

如何将Eclipse的控制台消息写入文件?从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"); 
     }  
    } 

回答

3

您需要更改 FileOutputStream("C:/Error.txt")

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

Juned Ahsan提到。除此之外,如果你需要写你从控制台进入

Error.txt

文本需要后

System.setOut(out);

线,在哪里添加一行

System.out.println(lineFromInput);您设置默认的输出位置。

+0

现在我改变了马代码像你说的,但它没有工作尝试{ \t \t \t \t \t \t \t \t的BufferedReader在=新的BufferedReader(新的InputStreamReader(System.in)); \t String lineFromInput = in.readLine(); (新的FileOutputStream(“C:\\ Error.txt”));}}; \t System.setOut(out); \t System.out.println(lineFromInput); \t out.close(); \t \t} \t \t赶上(IOException的E1){ \t \t的System.out.println( “期间读/写错误”); \t \t} – Anna

+0

@Anna你得到什么错误? – pinkpanther

+0

@pinkpanther我did not得到errors.but它不写控制台输出到文件 – Anna

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 
       } 
      } 
     } 
+0

该文件是使用此代码创建的,但其为空。 – Anna

+0

@Anna你在控制台上输入了你的文字并按下了回车? – Njax3SmmM2x2a0Zf7Hpd

+0

梅肯,实际上事情是在控制台上的消息是在运行代码之后生成的,之后该消息应该打印在文件上。 – Anna