File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes binary input and output? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "binary input and output?" Watch "binary input and output?" New topic
Author

binary input and output?

Lee Xu
Ranch Hand

Joined: Aug 24, 2001
Posts: 77
hi,
how can I do binary input and output from/into file? Thank you very much.
Lee
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
FileInputStream and FileOutputStream.
They should be used as opposed to FileReader and FileWriter because they use straight bytes rather than bytes that get translated to characters.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
... or RandomAccessFile. That's the lowest-level API for file access.
From the responses I see here, few people seem to use RandomAccessFile, whereas I've found it quite useful. Would anyone like to suggest why one wouldn't want to use RandomAccessFile?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
My experience is that RandomAccessFile is a lot slower than using FileOutputStream and FileInputStream. If the file can be processed linearly, RAF is a waste of time. Even for random access, I found that I could get better performance by creating a brand new FileOutputStream, using skip() to get to the desired position, reading from there, and then closing the reader.
Also, the readLine() method is basically useless if you need to support Unicode. I can create a BufferedReader wrapped around an InputStreamReader to read any encoding I want - not so with RAF. I mean, it's possible with RAF, but it's a lot more of a pain.
The fact that you can both read and write using the same class may seem like a benefit - but it generaly only works for wixed-length file formats. You can't replace a string with a longer string without overwriting subsequent data.
I'll concede RAF may be easier to use at first - but usually there's a better solution, for any given problem. IMO of course.


"I'm not back." - Bill Harding, Twister
 
 
subject: binary input and output?
 
Threads others viewed
XOR
How to export Non-English Data from jsp file to excel file
How do you display what's read from a file in a JTextArea properly?
ServletStream
readObject() throwing EOFException
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com