Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > writeChars not behaving as expected

Reply
Thread Tools

writeChars not behaving as expected

 
 
Ken Kafieh
Guest
Posts: n/a
 
      05-18-2004
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();




 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      05-18-2004
On Tue, 18 May 2004 03:03:12 GMT, Ken Kafieh wrote:
> 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 5. 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Yu SONG
Guest
Posts: n/a
 
      05-18-2004
Gordon Beaton wrote:
> On Tue, 18 May 2004 03:03:12 GMT, Ken Kafieh wrote:
>
>>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 5. 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

--
Song

More info.:
http://www.dcs.warwick.ac.uk/~esubbn/

 
Reply With Quote
 
Ken Kafieh
Guest
Posts: n/a
 
      05-18-2004
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?

"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:40a9b657$(E-Mail Removed)...
> On Tue, 18 May 2004 03:03:12 GMT, Ken Kafieh wrote:
> > 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 5. 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
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e



 
Reply With Quote
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      05-18-2004
Ken Kafieh wrote:

> 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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-18-2004
On Tue, 18 May 2004 12:33:57 GMT, Ken Kafieh wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Ken Kafieh
Guest
Posts: n/a
 
      05-18-2004
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?


"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 18 May 2004 12:33:57 GMT, Ken Kafieh wrote:
> > 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
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e




 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-18-2004
On Tue, 18 May 2004 03:03:12 GMT, "Ken Kafieh"
<kkafieh-StopSp@(E-Mail Removed)> wrote or quoted :

> RandomAccessFile out = new RandomAccessFile(outputFile,"rw");
>
> out.write('a');
> out.writeChars("XXX");
> out.write('b');


see http://mindprod.com/jgloss/fileio.

You are probably writing 16 bit chars, where you editor wants 8 bit.

Random access is normally not human-readable.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java String.replaceAll() not behaving as expected William Krick Java 4 12-18-2008 03:08 PM
UpdatePanel not behaving as expected DeekoVB5@gmail.com ASP .Net 2 02-18-2008 08:32 PM
<div style="display:inline;"> not behaving as expected Steve Richter ASP .Net 2 05-28-2007 11:48 PM
custom errors not behaving as expected ilyasdhin@googlemail.com ASP .Net 1 04-16-2007 10:32 AM
sys._getframe() not behaving as expected Skip Montanaro Python 0 12-01-2003 09:05 PM



Advertisments