• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Write CR/LF at the end of a line

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am writing a line and want to put curage return and a line feed at the end that would be flatform independent.
I tried "\r" for CR butthis adding ^M at each line
Can any one help me out?
Thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you mean by "CRLF that would be platform independent." "\n" will produce whatever is the traditional line ending for the host platform; one character on UNIX sytems (0x0A, or line feed); two characters on windows 0x0D 0x0A, or carriage return follwoed by line feed.) On Macintosh, at least in the old days, you'd get 0x0D alone!
Anyway, using '\n' will actually produce the right thing for the host platform. \r will indeed produce an extra carriage return (0x0D, or ^M) character. If you write \r\n on Windows, you'll actually get 0x0D 0x0D 0x0A in the file, but just 0x0D 0x0A on UNIX.
Now if what you're saying is that regardless of platform, you want to create files with a CRLF in them, then I think the thing to do would be to avoid using Writers and use OutputStreams, so you get binary, uninterpreted output, and then use the explicit Unicode values you want to see in the file(\u000D, \u000A) to print those exact characters. But not sure why you'd want to do this.
 
Nazmul Bhuiyan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that short problem description.
I am creating files where each line would be 128 bytes and should be terminated with a <CR/LF> (0D0A, designated as type H for hexadecimal).
I am creating this file in UNIX but will be used in Windows PC.
I need a platform independent way to add <CR/LF> at the end of each line.
I am using BufferedWriter for that as follows:
public void write(String str) throws IOException {
writer.write(str);
}
I�m passing a String into the writer to write it.
I want to pass another String that will add platform independent <CR/LF> at the end.
I could do it in the write method like:
public void write(String str) throws IOException {
writer.write(str);
writer.write(<CR/LF> // ???
}
Or pass with the String to the write method that will contain the <CR/LF>.
I tried (\u000D, \u000A) to assign to a String but java doesn�t like it.
Can you let me know how to solve this?
Thanks
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"\n" will produce whatever is the traditional line ending for the host platform; one character on UNIX sytems (0x0A, or line feed); two characters on windows 0x0D 0x0A, or carriage return follwoed by line feed.)
No, I think you're thinking of methods like println() (in PrintStream/PrintWriter), newLine() (in BufferedWriter), or System.getProperty("line.separator"). These all depend on the platform. However \n in Java always means exactly one character: Unicode value 0x000A, which in any common single-byte encoding gets translated into the single byte 0x0A.

Running this on my Windows box, I get a 100 byte file, indicating that each newline is one byte only.
Nazmul - personally I often just use the \n anyway (no \r), and when I want to look at a file I use a text editor which is smart enough to understand that \n indicates a new line, even though I'm on Windows. Basically this just means, don't use Notepad. TextPad is a good alternative.
But in many cases, your best bet is to use a PrintWriter - the println() method will use the correct line separator for your platform, with a minumum for hassle. But beware that sometimes you're on a Windows box generating a file that will be viewed from Unix, or Mac, or vice versa. (Whatever the term is for a three-way vice versa.) In these cases you just have to decide for yourself which is most appropriate for your application. Who is your customer, and what do they expect?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried (\u000D, \u000A) to assign to a String but java doesn�t like it.
Try
String str = "\r\n";
The problem with using \u000D, \u000A here is that the java compiler processes these escapes before doing anything else - so if you wrote
String str = "\u000D\u000A";
the compiler interprets that as
String str = "[new line here]";
or
String str = "
";
which is a syntax error. So \n and \r are your friends here.
[ July 17, 2003: Message edited by: Jim Yingst ]
 
Nazmul Bhuiyan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
I just found a way to do this by reseting the system property to "\r\n"
System.setProperty("line.separator","\r\n");
I am wating to see whether it pass test or not.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic