如何从Java文件中删除最后一个换行符?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (1346)

下面的代码从文件中删除所有换行符

trimm.replace("\n", "").replace("\r", "");

样本文本:

ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821

我的代码:

    String actual ="ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n" + 
            "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";
    try {
          File fout = new File("I:\\demo\\S2.txt");
          FileOutputStream fos = new FileOutputStream(fout);
          BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
          String trimm= actual;
/*            StringBuilder sb = new StringBuilder(trimm);
              int lastEnterPosition = trimm.lastIndexOf("\r\n");
              sb.replace(lastEnterPosition, lastEnterPosition + 1, "");
              trimm = sb.toString();*/
              trimm = trimm.replaceAll("[\n\r]+$", "");
              bw.write(trimm);
              bw.newLine();
              bw.close();
            } catch (FileNotFoundException e){
              // File was not found
              e.printStackTrace();
            } catch (IOException e) {
              // Problem when writing to the file
              e.printStackTrace();
            }
提问于
用户回答回答于

试试以下代码:

public static void main(String[] args) throws Exception {
    String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
            "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";

    System.out.println("---------------------------------------------------Before replace Start of the input---------------------------------------------------");
    System.out.println(trimm);
    System.out.println("---------------------------------------------------Before replace End of the input---------------------------------------------------");

    System.out.println("---------------------------------------------------After replace Start of the input---------------------------------------------------");
    trimm = trimm.replaceFirst("[\n\r]+$", "");
    System.out.println(trimm);
    System.out.println("---------------------------------------------------After replace End of the input---------------------------------------------------");

}

输出:

---------------------------------------------------Before replace Start of the input---------------------------------------------------
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821

---------------------------------------------------Before replace End of the input---------------------------------------------------
---------------------------------------------------After replace Start of the input---------------------------------------------------
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
---------------------------------------------------After replace End of the input---------------------------------------------------
以下是将你提供的代码修改后的:
public static void main(String[] args) throws Exception {
    String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
            "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";

    try {
        File fout = new File("path");
        FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        trimm = trimm.replaceAll("[\n\r]+$", "");
        bw.write(trimm);
        //bw.newLine();//<-----------------------note this
        bw.close();
    } catch (FileNotFoundException e) {
        // File was not found
        e.printStackTrace();
    } catch (IOException e) {
        // Problem when writing to the file
        e.printStackTrace();
    }
}

public static void main(String[] args) throws Exception {
    String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
            "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";

    Path path = Paths.get("path");

    try (BufferedWriter writer = Files.newBufferedWriter(path))
    {
        writer.write(trimm.replaceFirst("[\n\r]+$", ""));
    }
}

public static void main(String[] args) {
    String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
            "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";

    try {
        Files.write(Paths.get("path"), trimm.replaceFirst("[\n\r]+$", "").getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

用户回答回答于

字符串总是以\r\n结尾

String test = "your string that ends with a newline\r\n";

你可以用这样的方法

String eol = "\r\n";
int eolLen = eol.length();
String tmp = test.substring(0, test.length()-eolLen);
String check = test.substring(test.length() - eolLen);
if (!eol.equals(check)) {
    throw new Exception(String.format("Expected newline, found %s", check));
}

扫码关注云+社区

领取腾讯云代金券