2012-11-16 92 views
4

我有一個我寫的程序的輸出文件。它由FileWriter和BufferedWriter寫入。「 n」導出到.jar文件時不起作用

FileWriter errout = new FileWriter(new File("_ErrorList.txt")); 
    BufferedWriter out = new BufferedWriter(errout); 

後來我用類似的行寫入文件。

out.write("Product id:" + idin + " did not fetch any pictures.\n "); 

當我在Eclipse中簡單運行程序時,輸出文件格式正確,每條消息寫入一個新行。但是,當我導出到.jar文件時,它不再起作用,並將每條消息放在一行上,就好像「\ n」不起作用。

我使用了FileWriter的/ BufferedWriter將不正確,或者它不是在一個.jar文件的工作?

+1

替換'\ N'與'System.getProperty( 「line.separator」)' –

回答

5

您不應該直接使用'\ n'。請使用out.newLine()來引入換行符,或將BufferedWriter換行爲PrintWriter,並使用out.println()

不管怎樣,這與.jar文件無關。更有可能的是Eclipse很聰明並向你展示換行符,而操作系統則不行。

+0

好吧,使用out.newLine()固定的問題。感謝您的幫助 – user1816892

4

一,檢查行分隔符是否有效。使用@Andrew Thompson提供的System.getProperty(「line.separator」)。

如果你做了很多這方面編寫新線的另一個選項是來包裝你的BufferedWriter在一個PrintWriter。

FileWriter errout = new FileWriter(new File("_ErrorList.txt")); 
    BufferedWriter out = new BufferedWriter(errout); 

    PrintWriter printWriter = new PrintWriter(out); 
    printWriter.println("Product id:" + idin + " did not fetch any pictures."); 
+0

我知道**我手邊必須有一個'println',當我去找它時,我只是分心了另一個問題。 ;) –

+0

@AndrewThompson在那裏做過。我儘可能多地給你信用。 :) – tjg184

+0

*「我給了你儘可能多的信用。」*這相當於'比我應得的更多的信用'。 ;)+1給你的答案,順便說一句。 –