如何在使用JAVA将字符数组写入文件后添加新行

如何在使用JAVA将字符数组写入文件后添加新行,java,file,Java,File,在执行以下代码之后 String content = new String("CONSOLIDATED_UNPAID_code_" + code2 + "_" + countryCode2 + " = " + reason2); try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } 输出如下所示

在执行以下代码之后

 String content = new String("CONSOLIDATED_UNPAID_code_" + code2 + "_" + countryCode2 + " = " + reason2);
    try {
      fileOutputStream.write(content.getBytes());
    }
    catch (IOException e) {
      e.printStackTrace();
    }
输出如下所示:

CONSOLIDATED_UNPAID_code_64      _KE = Account Dormant-Refer to DrawerCONSOLIDATED_UNPAID_code_65      _KE = Wrong/Missing Account Number (EFT)CONSOLIDATED_UNPAID_code_66      _KE = Wrong/Missing Reference 
但是我想要像这样

CONSOLIDATED_UNPAID_code_64      _KE = Account Dormant-Refer to Drawer
CONSOLIDATED_UNPAID_code_65      _KE = Wrong/Missing Account Number (EFT)

请建议

我必须查看代码的其余部分才能准确地告诉您应该做什么,但您只需在字符串中使用字符“\n”。

您可以通过多种方式实现向文件添加新行,以下是两种方法:

  • 在字符串中添加\n,这将导致字符串的其余部分以新行打印
  • 使用PrintWriter的println方法打印新行中的每个字符串
  • 还请记住,使用记事本打开文件可能无法识别\n因此,如果不在新行中显示字符串的剩余部分,请尝试使用

    使用PrintWriter,如下所示:

    String line1 = "This is line 1.";
    String line2 = "This is line 2."; 
    
    File f = new File("C:\\test_stackoverflow\\test2.txt"); 
    PrintWriter out = new PrintWriter(f); 
    
    out.println(line1);
    out.println(line2);
    
    //close the output stream
    out.close();
    
    首先,在输出流的顶部使用一个字符串将字符串写入文件。这样,您就可以控制输出字符编码

    其次,如果要使用平台的行分隔符,可以使用which has
    println()
    方法,使用正确的换行符或字符序列

    PrintWriter writer = new PrintWriter(
        new OutputStreamWriter(fileOutputStream, OUTPUT_ENCODING)
    );
    
    ...
    
    writer.println(content);
    

    找到了解决方案。附加/n不会解决任何问题,而是使用BufferedWriter。BufferedWriter有一个内置的换行符mwthod来执行同样的操作。谢谢

    解决方案:

     try {
        File file = new File("Danny.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutputStream) );
        String content = new String("CONSOLIDATED_UNPAID_description_"+code2+"_"+countryCode2+" = "+description2);
        bw.write(content);
        bw.newLine();
        bw.flush();
        check = true;
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    

    为什么不在字符串末尾添加“\n”?
    如何添加新行…
    -就这样做。这里有一个“新行”字符
    \n
    。您不需要在字符串周围放置
    新字符串(…)
    。首先确保将\n添加到字符串中,然后您需要尝试使用不同的查看器打开文件(如记事本+)查看我的答案,我列出了另一种可用于逐行写入文件的方法。它只是在添加和显示的字符串后面添加\n。试过这个。
     try {
        File file = new File("Danny.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutputStream) );
        String content = new String("CONSOLIDATED_UNPAID_description_"+code2+"_"+countryCode2+" = "+description2);
        bw.write(content);
        bw.newLine();
        bw.flush();
        check = true;
      }
      catch (IOException e) {
        e.printStackTrace();
      }