Java has a bewildering array of I/O routines, many of them deprecated in JDK 1.1. 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. This document undoubtedly contains errors. Take anything I say with a large grain of salt and please report errors. If your code does not work, the most likely problem is confusion over whether you are processing a stream of 8-bit bytes or 16-bit chars.
With Java version 1.1 a new set of I/O classes have been added, Readers and Writers. Readers translate from bytes to Unicode internally and Writers do the reverse. The translate table used is configurable. In contrast, InputStreams and OutputStreams present bytes internally. There are almost no methods for dealing with external files of Unicode characters.
Consider the following sorts of file you might like to process in Java :
File Type | Suggested Classes and Methods |
---|---|
ASCII | InputStreamReader.read, DataInputStream.readByte, DataInputStream.read, PushbackReader.read, StreamTokenizer.nextToken, DataInputStream.readLine |
UTF | DataInputStream.readUTF |
Unicode | DataInputStream.readChar |
Java binary | DataInputStream.readInt etc. |
raw bytes | BufferedInputStream.read |
persistent objects | ObjectStream.readObject, ObjectStream.read |
File Type | Suggested Classes and Methods |
---|---|
ASCII | OutputStreamWriter.write, PrintWriter.print, PrintWriter.println formats, DataOutputStream.writeByte, DataOutputStream.write |
UTF | DataOutputStream.writeUTF |
Unicode | DataOutputStream.writeChars, writeChar |
Java binary | DataOutputStream.writeInt etc. |
raw bytes | BufferedOutputStream.write |
persistent objects | ObjectOutputStream.writeObject, ObjectOutputStream.write |
Make | Via | Purpose |
---|---|---|
Describe a File | ||
File | new File(String filename) eg. C:\\TEMP\\file.txt locale specific or C:/TEMP/file.txt |
mkdir, delete, isFile, renameTo |
FileDescriptor | FileDescriptor.in initSystemFD(new FileDescriptor(), 0 /* handle number */) |
input from the console keyboard. input from any other standard input handle. |
Choose a Source (Reader or InputStream) |
Readers for ASCII files, InputStreams for binary | |
FileReader | new FileReader(String filename) new FileReader(File file) new FileReader(FileDescriptor fdObj) new FileReader(FileInputStream fi) |
input comes from a file 8-bit ASCII (or other 8-bit encoding if the InputStreamReader is also used) and is converted to 16-bit Unicode. |
InputStreamReader | new InputStreamReader(FileInputStream file, translateTable t) |
input comes from a file of 8-bit chars and you want them translated to 16-bit Unicode for further processing by some other kind of Reader. |
CharArrayReader | new CharArrayReader(int size) | input comes from an array of 16-bit Unicode chars in RAM (Random Access Memory) |
StringReader | new StringReader(String s) | input comes from a String of 16-bit Unicode chars in RAM |
PipedReader | new PipedReader(PipedWriter src) | character input from a pipe usually to communicate between threads. |
FileInputStream | new FileInputStream(String filename) new FileInputStream(File file) new FileInputStream(FileDescriptor fdObj) |
input comes from a file of binary data |
ByteArrayInputStream | new ByteArrayInputStream(int size) | binary input comes from an array of bytes in RAM |
StringBufferInputStream | new StringBufferInputStream(int size) | binary input comes from a StringBuffer array of 16-bit Unicode chars in RAM |
PipedInputStream | new PipedInputStream(PipedOutputStream src) | binary input from a pipe usually to communicate between threads. |
Choose any Extras | ||
BufferedReader | new BufferedReader(FileReader in, int size) | add buffering to another character file I/O method so that read ahead in large chucks can efficiently serve many small I/Os. |
BufferedInputStream | new BufferedInputStream(FileInputStream in, int size) | add buffering to another binary 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 binary files making them appear as one. |
Choose a Format | ||
Reader | new FileReader(String filename) new InputStreamReader(FileReader file) new BufferedReader(FileReader in) |
reads 8-bit ASCII character input a character at a time, presenting it internally as 16-bit Unicode |
Reader | new CharArrayReader(int size) new StringReader(String s) new PipedReader(PipedWriter src) |
reads 16-bit Unicode character input a character at a time. |
LineNumberReader | new LineNumberReader(Reader in) | reads 16-bit Unicode character input organised in lines. |
PushBackReader | new PushBackReader(Reader in) | for parsing 16-bit Unicode character input, allows you to read-ahead one character and push it back for re-reading. |
InputStream | new FileInputStream (File in) new BufferedInputStream (InputStream in) new ByteArrayInputStream(int size) new StringBufferInputStream( int size) new SequenceInputStream (FileInputStream s1, FileInputStream s2) new PipedInputStream(PipedOutputStream src) |
raw bytes |
DataInputStream | new DataInputStream(InputStream in) | binary Input in big-endian format |
InputObjectStream | new InputObjectStream(InputStream in) | binary persistent object input in big-endian format. |
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 */) Unfortunately, this is private method. |
output goes to the console output to standard output handle. |
Choose a Target (Writer or OutputStream) |
Writers for ASCII files, OutputStreams for binary | |
FileWriter | new FileWriter(String filename) new FileWriter(File file) new FileWriter(FileDescriptor fdObj) |
output is converted from 16-bit Unicode to 8-bit ASCII, (or other 8-bit encoding if the OutputStreamWriter is also used.) |
OutputStreamWriter | new OutputStreamWriter(FileOutputStream file, encoding t) | An OutputStreamWriter is a bridge from 16-bit Unicode character streams to 8-bit ASCII streams: It translates characters into bytes according to a specified character encoding and writes the bytes to an output stream. The encoding that it uses may be specified by name, or the platform’s default encoding may be accepted. |
CharArrayWriter | new CharArrayWriter(int size) | output goes to an array of 16-bit Unicode characters in RAM |
StringWriter | new StringWriter(String s) | output goes to a String of 16-bit Unicode chars in RAM |
PipedWriter | new PipedWriter(PipedReader snk) | character output to a pipe usually to communicate between threads. |
FileOutputStream | new FileOutputStream(String filename) new FileOutputStream( String
filename, boolean append) new FileOutputStream(File file) new FileOutputStream(FileDescriptor fdObj) |
output goes to a file of binary bytes. If you use append, your output is tacked on the end of the file. If the file does not exist, it will be automatically created. |
ByteArrayOutputStream | new ByteArrayOutputStream(int size) | binary output goes to an array of bytes in RAM |
StringBufferOutputStream | new StringBufferOutputStream(int size) | binary output goes to a StringBuffer array of 16-bit Unicode chars in RAM |
PipedOutputStream | new PipedOutputStream(PipedInputStream snk) | binaryoutput to a pipe usually to communicate between threads. |
Choose any Extras | ||
BufferedWriter | new BufferedWriter(FileWriter out, int size) | add buffering to another character file I/O method so that small I/Os are saved up for efficiency. |
BufferedOutputStream | new BufferedOutputStream(FileOutputStream out, int size) | add buffering to another binary file I/O method so that small I/Os are saved up for efficiency. |
Choose a Format | ||
PrintWriter | new PrintWriter(FileWriter out, boolean autoflush) new PrintWriter(OutputStreamWriter out, boolean autoflush) new PrintWriter(CharArrayWriter out, boolean autoflush) new PrintWriter(PipedWriter out, boolean autoflush) |
formatted character output goes to a file of the default character encoding, usually 8-bit ASCII output. Has built-in buffering. |
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 |
OutputObjectStream | new OutputObjectStream(OutputStream out) | Binary output of persistent objects in big-endian format. Fields are identified with type information. When objects are written, all referenced objects get recursively written as well. |
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, then write. Only binary format is supported. Alternatively, you can use the FileOutputStream (String name, boolean append) constructor. |
Deprecated JDK 1.0.2 Class | new Java version 1.1 Replacement Class |
---|---|
BufferedInputStream | BufferedReader |
BufferedOutputStream | BufferedWriter |
ByteArrayInputStream | CharArrayReader |
ByteArrayOutputStream | CharArrayWriter |
FileInputStream | FileReader |
FileOutputStream | FileWriter |
FilterInputStream | FilterReader |
FilterOutputStream | FilterWriter |
LineNumberInputStream | LineNumberReader |
OutputStream | Writer |
PipedInputStream | PipedReader |
PipedOutputStream | PipedWriter |
PrintStream | PrintWriter |
PushbackInputStream | PushbackReader |
StringBufferInputStream | StringReader |
StringBufferOutputStream | StringWriter |
This page is posted |
http://mindprod.com/jgloss/io11.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\io11.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.97.14.84] |
| |
Feedback |
You are visitor number | |