aspose file tools*
The moose likes Java in General and the fly likes writing text file with RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "writing text file with RandomAccessFile" Watch "writing text file with RandomAccessFile" New topic
Author

writing text file with RandomAccessFile

Xinyi Zhang
Ranch Hand

Joined: Apr 28, 2001
Posts: 42
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();


Xinyi
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
> 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.


"I'm not back." - Bill Harding, Twister
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
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


Shubhrajit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing text file with RandomAccessFile