Remove the last newline from file in Java
You can use replaceFirst
which use regex with this one [\n\r]+$
, like so :
trimm = trimm.replaceFirst("[\n\r]+$", "");
Full codeI tried this piece of code :
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---------------------------------------------------");
}
The output :
---------------------------------------------------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---------------------------------------------------
Note that there are a break line before the replace and after the replace only the last break line is removed.
Ideone demo
More details
I tried this three solutions :
Code 1 (Your code)
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();
}
}
Code 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]+$", ""));
}
}
Code 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();
}
}
and all the three codes gives me :
Java: How to remove all line breaks between double quotes
You may match all substrings between double quotation marks using a simple "[^"]*"
regex and remove all linebreaks in between using
String s = "\"Test Line wo line break\"; \"Test Line \nwith line break\"\n\"Test Line2 wo line break\"; \"Test Line2 \nwith line break\"";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("\"[^\"]*\"").matcher(s);
while (m.find()) {
m.appendReplacement(result, m.group().replaceAll("\\R+", ""));
}
m.appendTail(result);
System.out.println(result.toString());
Or, beginning with the Java 9+, you can use a bit shorter code:
String s = "\"Test Line wo line break\"; \"Test Line \nwith line break\"\n\"Test Line2 wo line break\"; \"Test Line2 \nwith line break\"";
Matcher m = Pattern.compile("\"[^\"]*\"").matcher(s);
s = m.replaceAll(r -> m.group().replaceAll("\\R+", ""));
System.out.println(s);
Output:
"Test Line wo line break"; "Test Line with line break"
"Test Line2 wo line break"; "Test Line2 with line break"
See the Java demo online / Java code demo #2.
Note that .replaceAll("\\R+", "")
finds 1 or more any line break sequences and removes them only from what "[^"]*"
matched.
Escape sequence support between double quotation marks
If your strings between double quotes can contain escaped sequences you need to use a different pattern:
Pattern.compile("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"", Pattern.DOTALL)
Note the Pattern.DOTALL
option, it will allow .
to match line break chars.
Details:
"
- a"
char[^"\\]*
- zero or more chars other than"
and\
chars(?:\\.[^"\\]*)*
- zero or more sequences of a\
and any char after it followed with zero or more chars other than"
and\
chars"
- a"
char.
How remove the last brak line from the txt file with JAVA?
You can delete the last line using the method described in the post here. But as you may know the EOL (line break) is 2 character long \r\n
in windows systems CRLF
and 1 character long in macOS/linux systesm \n
LF
, so you need to delete the last 2 characters if your file is windows formated :
try {
FileChannel open = FileChannel.open(Paths.get("file.tx"), StandardOpenOption.WRITE);
open.truncate(open.size() - 2); //the 2 characters I was describing above
} catch (IOException e) {
System.out.println("An IO error occurred.");
e.printStackTrace();
}
Keep in mind that every time you run this code, the last 2 characters will be deleted, so it might be wise for you to implement a check that the EOL are the last characters of your file. You can do that by reading the last characters:
FileChannel read = FileChannel.open(Paths.get("file.txt"), StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(4);
read.read(buffer, read.size() - 2);
if(new String(buffer.array()).contains("\r\n")) {
FileChannel write = FileChannel.open(Paths.get("file.txt"), StandardOpenOption.WRITE);
write.truncate(write.size()-2);
}
Related Topics
Alternative to Switch Case in Java
Connection Java - Mysql:Public Key Retrieval Is Not Allowed
Reading Files With Intellij Idea Ide
How to Find Index of First Vowel in a String
Remove Quote from the Jsonarray Output
No Content to Map Due to End-Of-Input Jackson Parser
Getting 400 for Spring Resttemplate Post
How to Read File from Relative Path in Java Project? Java.Io.File Cannot Find the Path Specified
Regex to Validate 3 Repeating Characters
Transaction Marked as Rollback Only: How to Find the Cause
Disable Quick Settings Tile in Android
Issue With Java 8 Collectors Type Mismatch: Cannot Convert from List<Object> to List<String>
Spring @Transaction Method Call by the Method Within the Same Class, Does Not Work
How to Print the List of Employees Who Have Joined on a Particular Month from an Employee List
How to Activate a Function When Some Variable Changes