2014-03-02 69 views
0

我試圖讓BufferedWriter打印幾個字符串,每個在文本文件中的不同行上。我試圖用out.newLine()設置爲下一個字符串的新線,但我得到的cannot find symbol - method newLine()BufferedWriter newLine方法未找到

此錯誤消息是代碼我想使用:

Writer out = null; 
try { 
     out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(start+"2"+dest+".txt"), "utf-8")); // create output file from start names 
     out.write(outputOne);//Write Line 1 
     out.newLine(); 
     out.write(outputTwo); //Write Line 2 
     out.newLine(); 
     out.write(outputThree); //Write Line 3 
     out.newLine(); 
     out.write(outputFour); //Write Line 4 
     out.close(); 
} catch (IOException ex) { //Handle Errors 
      System.err.println("Error in BufferedWriter, IOException"); 
} finally { 
       try {out.close();} 
       catch(Exception ex) {} 
} 

回答

4

的聲明變量的類型爲BufferedWriterWriter沒有newLine()方法。

BufferedWriter out; 

方法在編譯時基於它們被調用的變量(或表達式)的聲明/靜態類型來解析。

或者,鑄可變

((BufferedWriter)out).write(outputFour); 

但這是長。


考慮使用try-with-resources