gpt4 book ai didi

java - 从 Java 文件中删除最后一个换行符

转载 作者:行者123 更新时间:2023-11-30 07:46:10 25 4
gpt4 key购买 nike

我想使用 java 删除文件中的最后一个换行符。我的意思是,文件末尾有一个换行符,我想将其删除。

我尝试了很多网上提供的解决方案,但没有任何效果。

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

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

上面的示例末尾有换行符。我引用了以下网址:

http://www.avajava.com/tutorials/lessons/how-do-i-remove-a-newline-from-the-end-of-a-string.html

https://www.java-forums.org/new-java/22655-removing-last-blank-line-txt-file.html

我不能在 \n 之后使用 split(),因为很多 raws 都有相同的词

我的代码:

    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();
}

任何解决方法都会有所帮助。

最佳答案

您可以使用 replaceFirst,它使用正则表达式 [\n\r]+$,像这样:

trimm = trimm.replaceFirst("[\n\r]+$", "");

完整代码

我试过这段代码:

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---------------------------------------------------

请注意,替换之前有一个换行符,替换之后只有最后一个换行符被删除。

Ideone demo


更多详情

我尝试了这三种解决方案:

代码 1(您的代码)

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();
}
}

代码2

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]+$", ""));
}
}

代码3

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();
}
}

所有三个代码都给了我:

result

关于java - 从 Java 文件中删除最后一个换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50880394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com