Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to delete new line characters in java

 
Ayan Biswas
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic