writechars in RandomAccessFile

HI,

I have a problem using writechars.

einDatei = new RandomAccessFile("einFile.txt","r");

ausDatei = new RandomAccessFile("ausFile.txt", "rw");

einDatei.seek(0);

s = einDatei.readLine();

ausDatei.writeChars(s);

s = einDatei.readLine();

ausDatei.writeChars(s);

einDatei.close();

ausDatei.close();

einFile.txt looks like :

"Hello World!

Hello world!"

ausFile.txt looks like :

" H e l l ow o r l d ! H e l l ow o r l d ! "

What is wrong?

thanks for help

[564 byte] By [Thraxasa] at [2007-10-3 4:01:39]
# 1

> What is wrong?

Nothing.

readLine() reads a line of text and converts each byte into a

character using 0 as the value for the character's upper 8 bits.

writeChars() uses the writeChar() method. This one writes each

character as a two byte value, high byte first.

So these methods are not the opposite of one another, and you

shouldn't expect to get the same file you started with. Full details in

the API docs: http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html

Keep in mind that the Java char is a 16 bit Unicode character.

(http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html)

pbrockway2a at 2007-7-14 22:00:54 > top of Java-index,Java Essentials,Java Programming...
# 2
For the most part, chars are two bytes wide. I expect that instead of ausDatei.writeChars(s);you should useausDatei.write(s.getBytes());P.S. Though you can use RandomAccessFile with text files it does not normally make sense.
sabre150a at 2007-7-14 22:00:54 > top of Java-index,Java Essentials,Java Programming...
# 3
Ok, I see.But is there any method I can use to get the same?
Thraxasa at 2007-7-14 22:00:54 > top of Java-index,Java Essentials,Java Programming...
# 4
ups.I didn't recognized the second answer.thanks
Thraxasa at 2007-7-14 22:00:54 > top of Java-index,Java Essentials,Java Programming...