How to write newline character to file using bytes along with content

Hi All,

I need to write headers into the file using byte[]. I am creating the strings of header which will form the different rows in the file. I am new to java and would like to know how to embed new line character so that when the byte[] is written to file headers come in different rows.

like

hrd1:abcd

hdr2:1234

when i embed \n and \r in string and do getBytes() and write it to file, some boxes are written in the file and everything comes into single line.

Please advice me the correct way .

Thanks in advance.

Cheers!!

Puneet

[597 byte] By [puneet_ka] at [2007-11-27 6:00:11]
# 1
Define "newline character" - if you just want that, that's '\n' and nothing else. And what are you calling getBytes() on?
CeciNEstPasUnProgrammeura at 2007-7-12 16:38:01 > top of Java-index,Java Essentials,New To Java...
# 2

> what are you calling getBytes() on?

he's calling getBytes() on his String object.

@ puneet_k,

I don't have a solution but note that on Unix-like systems a newline is a single byte 0x0a and on DOS-like systems (Windows) it is a sequence of bytes 0x0d and 0x0a.

Some text editors handle both.

I'm not sure but I think on Unix,

new String("\n").getBytes() equals new byte[] { 0x0a }

and on DOS

new String("\n").getBytes() equals new byte[] { 0x0d, 0x0a }

tom_jansena at 2007-7-12 16:38:01 > top of Java-index,Java Essentials,New To Java...
# 3
> I am new to javayou don't usually do TEXT file I/O with bytes[].Have a geezer at the tutorials at: http://java.sun.com/docs/books/tutorial/essential/io/
corlettka at 2007-7-12 16:38:01 > top of Java-index,Java Essentials,New To Java...
# 4
This line of code will work on every platform:System.getProperty("line.separator").getBytes()
Dalzhima at 2007-7-12 16:38:01 > top of Java-index,Java Essentials,New To Java...