File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes how to delete new line characters in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to delete new line characters in java" Watch "how to delete new line characters in java" New topic

how to delete new line characters in java

Ayan Biswas
Ranch Hand

Joined: Jul 10, 2010
Posts: 104
I want to delete newline character from a text file.Surely,a straight forward way would be to read the characters one by one and write it in another file,if it finds a newline it would not write it in the second file.But I just want to delete the newline character in the first file without using any second file.How to do it?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
If you use line-reading methods, eg those of Scanner and BufferedReader, you get Strings with line-end characters removed. Note there are significant differences between those two methods.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Basically a file is modelled as an array of bytes. This isn't just a Java idea, this is how operating systems look at files.

So if you want to delete one of the bytes in a file, you then have to move all of the bytes which are to the "right" of that byte one position to the "left". This can be tedious if the file is very large, or if you're accessing it over a network. That's why everybody just does it the straightforward way, copy only the data you want to a new file.
I agree. Here's the link:
subject: how to delete new line characters in java
It's not a secret anymore!