我想使用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
split()
之后我无法使用\n
,因为很多原始词都有相同的词
我的代码:
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();
}
任何解决方法都会有所帮助。
答案 0 :(得分:2)
您可以使用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---------------------------------------------------
输出:
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();
}
}
self.tab1.MyLineEdit
self.tab2.MyLineEdit
并且所有三个代码都给了我:
答案 1 :(得分:1)
如果您知道字符串将始终以\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));
}