Java has a bewildering array of I/O routines. Many you can plug together like Lego blocks to create variants. The following tables will give you some candidate methods for the task at hand. You will have to read Oracle’s documentation to see which possibility is best. Consider the following sorts of file you might like to process in Java :
What classes and methods would be most appropriate to READ each flavour of file?
ASCII | DataInputStreamReader.readChar, DataInputStream.readByte, DataInputStream.read, PushbackInputStream.read, StreamTokenizer.nextToken, DataInputStream.readLine |
UTF | DataInputStream.readUTF |
Unicode | DataInputStream.readChar, valueOf |
Java binary | DataInputStream.readInt etc. |
raw bytes | BufferedInputStream.read |
What classes and methods would be most appropriate to WRITE each flavour of file?
ASCII | PrintStream.print, PrintStream.println, DataOutputStream.writeByte, DataOutputStream.write |
UTF | DataOutputStream.writeUTF |
Unicode | DataOutputStream.writeChars, toString, valueOf |
Java binary | DataOutputStream.writeInt etc. |
raw bytes | BufferedOutputStream.write |
The various features of the Java I/O system can be plugged together like Lego blocks. This table will help you sort this all out by showing how you create each sort of object that you need to create the next higher level object. This should be a little easier to understand that tracing the class hierarchy. The table is not complete. Only the most likely techniques are shown.
Make | Via | Purpose |
---|---|---|
Describe a File | ||
File | new File(String filename) eg. C:\\TEMP\\file.txt locale specific |
mkdir, delete, isFile, renameTo |
FileDescriptor | FileDescriptor.in initSystemFD(new FileDescriptor(), 0 /* handle number */) Unfortunatly, this is a private method. |
input from the console keyboard input from other standard input handle. |
Choose a Source (InputStream) | ||
FileInputStream | new FileInputStream(String filename) new FileInputStream(File file) new FileInputStream(FileDescriptor fdObj) |
input comes from a file |
InputStream | System.in | input from the console keyboard. |
ByteArrayInputStream | new ByteArrayInputStream(int size) | input comes from an array of bytes in RAM (Random Access Memory) |
StringBufferInputStream | new StringBufferInputStream(int size) | input comes from a StringBuffer array of Unicode chars in RAM |
PipedInputStream | new PipedInputStream(PipedOutputStream src) | input from a pipe usually to communicate between threads. |
Choose any extras | ||
BufferedInputStream | new BufferedInputStream(FileInputStream in, int size) | add buffering to another file I/O method so that read ahead in large chucks can efficiently serve many small I/Os. |
SequenceInputStream | new SequenceInputStream(FileInputStream s1, FileInputStream s2) | logically concatenates two files making them appear as one. |
Choose a Format | ||
DataInputStream | new DataInputStream(InputStream in) |
binary Input in big-endian format |
InputStream | new FileInputStream(File file) System.in new ByteArrayInputStream(int size) new StringBufferInputStream(int size) new PipedInputStream(PipedOutputStream src) |
Reads ASCII 8-bit characters. |
LineNumberInputStream | new LineNumberInputStream(InputStream in) |
reads ASCII 8-bit character input organised in lines. |
PushBackInputStream | new PushBackInputStream(InputStream in) |
for parsing ASCII 8-bit character input, allows you to read-ahead one character and push it back for re-reading. |
Make | Via | Purpose |
---|---|---|
Describe a File | ||
File | new File(String filename) | mkdir, delete, isFile, renameTo |
FileDescriptor | FileDescriptor.err FileDescriptor.out initSystemFD(new FileDescriptor(), 1 /* handle number */) |
output goes to the console. output to other standard output handle. |
Choose a Target (OutputStream) | ||
FileOutputStream | new FileOutputStream(String filename) new FileOutputStream(File file) new FileOutputStream(FileDescriptor fdObj) |
output goes to a file If you open an existing file, data is overwritten. |
PrintStream | System.out System.err |
output goes to the console |
ByteArrayOutputStream | new ByteArrayOutputStream(int size) | output goes to an array of bytes in RAM |
PipedOutputStream | new PipedOutputStream(PipedInputStream snk) | output to a pipe usually to communicate between threads. |
Choose any extras | ||
BufferedOutputStream | new BufferedOutputStream(FileOutputStream out, int size) | add buffering to another file I/O method so that small I/Os are saved up for efficiency. Buffering automatically used by PrintStream. |
Choose a Format | ||
OutputStream | new FileOutputStream(File file) new ByteArrayOutputStream(int size) new PipedOutputStream(PipedInputStream snk) |
raw bytes |
DataOutputStream | new DataOutputStream(OutputStream out) | binary output in big-endian format |
PrintStream | new PrintStream(OutputStream out, boolean autoflush) | human-readable 8-bit ASCII output |
Make | Via | Purpose |
---|---|---|
Describe a File | ||
File | new File(String filename) | mkdir, delete, isFile, renameTo |
Choose a Source | ||
RandomAccessFile | new RandomAccessFile( String filename, String mode /* e.g. rw */) new RandomAccessFile( File file, String mode) |
random access read and write using seek to position. Useful to append onto the end of an existing file; use length() to find out how long the file is, then seek to the end of the file then write. Only binary format is supported. |
This page is posted |
http://mindprod.com/jgloss/io10.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\io10.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[18.225.254.81] |
| |
Feedback |
You are visitor number | |