• Post Reply Bookmark Topic Watch Topic
  • New Topic

Forcing CRLF as line terminator in file output  RSS feed

 
L Duperval
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I feel foolish asking this but...
What is the best way to ensure that text files contain CRLF as a line terminator when writing to a file? It should work identically on Windows and Unix. It's possible that the data will be written to the file on Unix, and I don't want the newline character to be <LF> only.
I thought about forcing a change to the line.separator property but I think that's kind of a sledgehammer approach. I didn't find any indication of another way to do it. I thought that maybe I could just define a separator as a String of two characters: \u000A and \u000D. But I'm not sure if that's the right way to go.
BTW, I don't have a problem with just having <LF> as a line terminator but on Windows, it causes problems when the file is opened in a weak editor (Notepad, for example).
Thanks,
L
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware of any problem associated with simply explicitly printing CR and LF characters at the end of each line. Note that CR is character 13 and LF is character 10.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write "\r\n" at the end of each line. I'm pretty sure that works for both unix and windows.
brian
 
L Duperval
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers. After rereading the docs, I'm wondering where I got the idea that every write automatically added a line-ending character that differed depending on the platform used.
L
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps from the documentation for PrintStream and PrintWriter, since they do exactly this. But that's not the only way to write a line, as you see.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!