Want to remove line breaks from a file in Java? You need to set text to the results of text.replace():
String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");
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.