我试图获得一个BufferedWriter来打印几个字符串,每个字符串都在文本文件的不同行上。我正在尝试使用out.newLine()为下一个字符串设置新行,但是我收到了cannot find symbol - method 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) {}
}

最佳答案

BufferedWriter类型声明变量。 Writer没有newLine()方法。

BufferedWriter out;


方法在编译时根据调用它们的变量(或表达式)的声明/静态类型进行解析。

或者,强制转换变量

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


但这很长。



考虑使用try-with-resources

关于java - 找不到BufferedWriter newLine方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22122882/

10-11 07:05