How to remove line breaks from a file in Java?

Posted by Pinki | Updated on

Want to remove line breaks from a file in Java? You need to set text to the results of text.replace():

This is necessary because Strings are immutable — calling replace doesn’t change the original String, it returns a new one that’s been changed. If you don’t assign the result to text, then that new String is lost and garbage collected.

As for getting the newline String for any environment that is available by calling System.getProperty(“line.separator”).


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers