Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing text file with RandomAccessFile

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to write "text" to text file, but "t e x t" was written instead of "text". Why?
---------------------------------------
RandomAccessFile file = new RandomAccessFile("c:\\myFolder\\myFile.txt", "rw");
file.writeChars("text");
file.close();
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting...I got the same output on Solaris and Windows -- an 8-byte file. On Windows, however, those extra characters, which are NUL bytes, not whitespace in the usual sense, get displayed. On Solaris they don't.
writeChars() relies on writeChar(), so we should expect that the high-order eight bits are get discarded, as the API docs say. But a Java character is still two bytes in length, so I wonder...
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> writeChars() relies on writeChar(), so we
> should expect that the high-order eight
> bits are get discarded, as the API docs say
You're thinking of writeBytes(String), which discards the high-order byte. But writeChars() does not - it's performing as specified. It's just that the 0x00 bytes are displayed differently on the different systems. (And different viewing tools, probably.)
For the original poster - this is just one of several reasons I avoid the RandomAccessFile class most of the time. The API tries to do too many things, and few of them very well. Usually there's another specialized class that does what you want better. To read and write text, consider a Reader and Writer instead.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
Interesting...I got the same output on Solaris and Windows -- an 8-byte file. On Windows, however, those extra characters, which are NUL bytes, not whitespace in the usual sense, get displayed. On Solaris they don't.
writeChars() relies on writeChar(), so we should expect that the high-order eight bits are get discarded, as the API docs say. But a Java character is still two bytes in length, so I wonder...



Java writes to the file as 16 bits per character. I think it is better to use byte level writing.
Java File APis are horrible. Once I had a requirement to store some updatable data but the client was not ready to buy a database. I wasted a lot of time on random access file but could not do it ultimately. In the end I wrote the data to a Vector serialized it at wrote it to a persistent store and it worked like a fly.
I wrote all this because I thought that you might have a sim. req. like me. If all you are doing is writing reports, or sim. sort of stuff, then use Writer ( I like PrintWriter ) objects.

------------------
Shubhrajit
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic