I/O in JDK 1.0 : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

I/O in Java 1.0
I/O was revamped in Java version 1.6 or later to provide localisation. If you are interested in the file I/O in the newer  Java version 1.6 or later or are interested in deprecated methods, have a look at the Java version 1.6 or later essay. The essay you are now reading only covers JDK (Java Development Kit) 1.0.2.

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 :

  1. 7 or 8-bit ASCII (American Standard Code for Information Interchange) characters. Newlines are marked by a single \n character, not the usual \r\n seen in DOS/Windows. If you want the \r you have to put it in yourself. The CMP (Canadian Mind Products) TabIn or TabOut utility will insert the needed \r’s.
  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

Reading Methods

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
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?

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 most common error is to try to use methods designed for 16-bit Unicode on 8-bit ASCII files created outside Java.

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 that 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
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.

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 */)
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

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 of the file then write. Only binary format is supported.

This page is posted
on the web at:

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

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

J:\mindprod\jgloss\io10.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.225.254.81]
You are visitor number