I/O in JDK 1.1+ : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

I/O in Java 1.1+
I/O was revamped in Java version 1.1 to provide localisation. If you are interested in the older JDK (Java Development Kit) 1.0.2 I/O have a look at the JDK 1.0.2 essay. The essay you are now reading only covers JDK 1.1.

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 :

  1. 7 or 8-bit ASCII (American Standard Code for Information Interchange) characters. There is a problem with what to use for line endings. Unix uses \n, Windows \r\n. PrinterWriter.println is smart enough to follow local conventions, but embedded \n characters are not automatically expanded to \r\n. The CMP (Canadian Mind Products) TabIn or TabOut utility will insert the needed \r’s. You can use code like this
    // \r\n for W95/NT, \r for Mac and \n for Unix
    static String LINEEND = System.getProperty( line.separator );
    and emit the appropriate line end string rather than use println.
  2. UTF (Unicode Transformation unit) 8-bit Unicode compressed character encoding
  3. 16-bit Unicode characters
  4. Java binary format-- platform independent big-endianbinary representations
  5. raw bytes
  6. persistent objects

Reading Methods

What classes and methods would be most appropriate to READ each flavour of file?
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
DataInputStream.readLine will return "" for a null line in a file and a null when you hit EOF (End Of File). It won’t raise an EOFException! It just keeps returning null. The string that readLine returns does not include the terminating \n, \r or \r\n pair.

Writing Methods

What classes and methods would be most appropriate to WRITE each flavour of file?
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

How To Create I/O Control Objects

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 than tracing the class hierarchy. The table is not complete. Only the most likely techniques are shown.

Input

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.

Output

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.

Random Access Input/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, then write. Only binary format is supported. Alternatively, you can use the FileOutputStream (String name, boolean append) constructor.

File Length

To get the length of a file:
File f = new File( C:/TEMP/people/temp.txt );
long length = f.length ();
Note that the available() method just gets how many bytes are immediately available for the next I/O without blocking.

I/O Redirection

You can redirect the standard error, in or out streams to a file or other stream this way:
System.setErr(PrintStream err);
System.setIn(InputStream in);
System.setOut(PrintStream out);
// You can also do it with a mild cheat
System.out = someotherStream;

Deprecation

Some of the methods in the following classes in JDK 1.0.2 may have been deprecated. You might consider replacing them with the enhanced Java version 1.1 classes according to this table, if you are doing character i/o.The old classes are fine for binary i/o.
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
on the web at:

http://mindprod.com/jgloss/io11.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\io11.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.97.14.84]
You are visitor number