writeChars not behaving as expected

Discussion in 'Java' started by Ken Kafieh, May 18, 2004.

  1. Ken Kafieh

    Ken Kafieh Guest

    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();
     
    Ken Kafieh, May 18, 2004
    #1
    1. Advertising

  2. 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 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

    --
    [ 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
     
    Gordon Beaton, May 18, 2004
    #2
    1. Advertising

  3. Ken Kafieh

    Yu SONG Guest

    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 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

    --
    Song

    More info.:
    http://www.dcs.warwick.ac.uk/~esubbn/
     
    Yu SONG, May 18, 2004
    #3
  4. Ken Kafieh

    Ken Kafieh Guest

    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" <> wrote in message
    news:40a9b657$...
    > 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 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
    >
    > --
    > [ 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
     
    Ken Kafieh, May 18, 2004
    #4
  5. 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
     
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, May 18, 2004
    #5
  6. 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
     
    Gordon Beaton, May 18, 2004
    #6
  7. Ken Kafieh

    Ken Kafieh Guest

    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" <> wrote in message
    news:...
    > 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
     
    Ken Kafieh, May 18, 2004
    #7
  8. Ken Kafieh

    Roedy Green Guest

    On Tue, 18 May 2004 03:03:12 GMT, "Ken Kafieh"
    <kkafieh-StopSp@> 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.
     
    Roedy Green, May 19, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Krick
    Replies:
    4
    Views:
    77,866
    rt19983
    Dec 18, 2008
  2. Skip Montanaro

    sys._getframe() not behaving as expected

    Skip Montanaro, Dec 1, 2003, in forum: Python
    Replies:
    0
    Views:
    357
    Skip Montanaro
    Dec 1, 2003
  3. Replies:
    1
    Views:
    348
    David
    Apr 16, 2007
  4. Steve Richter
    Replies:
    2
    Views:
    608
    Steve Richter
    May 29, 2007
  5. Replies:
    2
    Views:
    433
Loading...

Share This Page