• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to remove carriage returns when writing to a text file

 
Jonathan Haybok
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then can someone tell me why this isn't removing carriage returns when writing to a text file? I'm new to I/O.



 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is not in IO area, but in regular expressions area. Different programs even within single operating system could have different line separators. So to replace them all use not

but
 
Greg Charles
Sheriff
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean your code is replacing new lines, but not carriage returns? That's probably because your system line.separator is just a new line, but your Strings have both carriage return and new line. Try changing to:


and see if that works. Even better would be to come up with a regular expression that would match all possible line endings you might get embedded into those strings.

ETA: Oh, well, Andrey beat me to it. I believe that solution would also convert a tab to a space though, and maybe multiple spaces to a single space, which may or may not be a problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic