writeChars not behaving as expected

K

Ken Kafieh

This code is trying to create a file with the following content: aXXXb
But it doesn't. It just stops writing the file after it writes 'a' to it.
any hints?

File outputFile = new File(tmpfile);

RandomAccessFile out = new RandomAccessFile(outputFile,"rw");

out.write('a');
out.writeChars("XXX");
out.write('b');

out.close();
 
G

Gordon Beaton

This code is trying to create a file with the following content:
aXXXb But it doesn't. It just stops writing the file after it writes
'a' to it. any hints?

File outputFile = new File(tmpfile);

RandomAccessFile out = new RandomAccessFile(outputFile,"rw");

out.write('a');
out.writeChars("XXX");
out.write('b');

out.close();

It writes file with the following byte contents:

61 00 58 00 58 00 58 62

Perhaps those zeroes are confusing you.

Note that writeChars() and writeChar() write two bytes per character
(e.g. 00 58). writeBytes() doesn't do that.

If you don't strictly need a RandomAccessFile, consider using a
FileOutputStream together with an OutputStreamWriter. Don't forget to
specify the character encoding you want to use.

/gordon
 
Y

Yu SONG

Gordon said:
It writes file with the following byte contents:

61 00 58 00 58 00 58 62

Perhaps those zeroes are confusing you.

Note that writeChars() and writeChar() write two bytes per character
(e.g. 00 58). writeBytes() doesn't do that.

If you don't strictly need a RandomAccessFile, consider using a
FileOutputStream together with an OutputStreamWriter. Don't forget to
specify the character encoding you want to use.

/gordon

Or use FileChannel
 
K

Ken Kafieh

When I opened the file in a text editor, it simply showed an 'a'
Are you saying that the characters were actual in the file but were
invisible when using a text editor?
Is there a way to view the contents? hmmm
I guess a hex editor would be good? can anyone recommend one?
 
G

Guest

Ken said:
When I opened the file in a text editor, it simply showed an 'a'
Are you saying that the characters were actual in the file but were
invisible when using a text editor?
Is there a way to view the contents?

I use emacs since 20 years.
No problem with emacs to see binary zeros in a file.

- Dario
 
G

Gordon Beaton

When I opened the file in a text editor, it simply showed an 'a' Are
you saying that the characters were actual in the file but were
invisible when using a text editor? Is there a way to view the
contents? hmmm

For starters, you could try something simple like this:

FileInputStream fis = new FileInputStream(tmpfile);
int ch;

while ((ch = fis.read()) != -1) {
System.out.print(ch + " ");
}
fis.close();

I can't explain the behaviour of your text editor, but mine (emacs)
shows the entire contents of the file.

/gordon
 
K

Ken Kafieh

Its because I am on windows.

would the fact that I am using 16 bit output also help explain why it is
having trouble copying new line characters as well?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top