Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing text file with RandomAccessFile

 
Xinyi Zhang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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();
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic