Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the last newline from file in Java

Tags:

java

string

file

I want to delete the last newline present in my file using java. I mean, there is a newline at the very end of the file, which I want to remove.

I have tried many solutions provided online, but nothing works.

The below code removes all newlines from file

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

Sample text:

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

The above sample has newline at the end. I have referred the URLs below:

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

I can't use split() after \n as many raws have same word

My code:

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

Any workaround will be helpful.

like image 398
Shubham Jain Avatar asked Feb 21 '26 19:02

Shubham Jain


1 Answers

You can use replaceFirst which use regex with this one [\n\r]+$, like so :

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

Full code

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

result

like image 130
YCF_L Avatar answered Feb 23 '26 07:02

YCF_L