The Java File Operations
Interfaces and classes for dealing with files and other I/O types are in the java.io package. An interface is a class that contains abstract methods. Classes in java.io form a class hierarchy.
The two main class types in java.io are text oriented (character streams) and binary oriented (byte streams). Subclasses of the Reader and Writer classes are text oriented; those of the InputStream and OutputStream classes are binary oriented.
InputStream and OutputStream are abstract; that is, they can't be instantiated directly. To use an abstract class you must subclass it and instantiate the subclass. The subclasses of InputStream and OutputStream allow the reading of binary data to and from various types of input and output such as byte arrays (memory), files, and even network sockets.
Streams can be chained to provide extra functionality. For example, you can buffer a FileInputStream by chaining it to a BufferedInputStream Then you can chain the BufferedInputStream to an ObjectInputStream to read in whole objects at one time. (This is similar to the pickle functionality in Python.)
Java 1.1's binary data input streams are complemented by somewhat equivalent text input streams. The parents of these classes are the abstract classes Reader and Writer.
Having an equivalent set of text-oriented character stream classes allows the conversion of Unicode text. Readers and Writers, like streams, can be chained together. For example, you can buffer a FileReader by chaining it to a BufferedReader. (Buffering will be explained shortly.)
I/O Classes to Be Covered.
There are more than thirty I/O classes, not including interfaces and abstract classes. This seems like a lot, but if you understand how Reader, Writer, InputStream, and OutputStream work, you can easily understand the rest.
Reader and Writer subclasses deal with character streams, that is, text. InputStream and OutputStream subclasses deal with binary streams. An easy way to remember this is, if you can read it, use Reader and Writer; if you can't, use InputStream and OutputStream.
All of the text-oriented classes are derived from Reader and Writer. By understanding them, you'll have a handle on dealing with their descendents.
The Writer class writes character data to a stream. It has the following methods:
- write(c) — writes a single character to the text stream
- write(cbuf) — writes a sequence of characters to the text stream
- write(cbuf, off, len) — writes a sequence of characters to the text stream starting at the offset into the stream and copying to len in the buffer
- write(str) — writes out a string to the text stream
- write(str, off, len) — writes out a string from the given offset to the given length
- close() — closes the text stream
- flush() — flushes the content of the text stream; used if the stream supports BufferedOutput
Writer is the superclass of all character output streams, for example, FileWriter, BufferedWriter, CharArrayWriter, OutputStreamWriter, and PrintWriter.
The Reader class reads data from a stream. Reader is the superclass of all character input streams. It has the following methods:
- read() — reads in a single character
- read(cbuf) — reads in a sequence of characters
- read(cbuf, iOff, iLen) — reads from the current position in the file into the sequence at the given offset
- ready() — determines if the buffer has input data ready
- mark(int readAheadLimit) — sets the read-ahead buffer size
- markSupported() — returns true if the mark() method is supported
- skip() — skips a certain number of characters ahead in the text input stream
- reset() — moves the file pointer to the marked position
- close() — closes the file
I can't show you any examples of using Reader or Writer because they're abstract and can't be instantiated on their own. However, I can show examples of their subclasses, FileReader and FileWriter, which are concrete.
As an exercise, look up Reader and Writer in the Java API documentation, and compare their Python-friendly method definitions to the official Java versions. Notice the conversion from one type to another.
To get help from the java professionals, sign up for the hosting account trial at our java hosting provider
FileReader and FileWriter
FileReader and FileWriter read and write text files. They have the same methods their base classes have as well as the following constructor methods.
__init__(strFilename)— opens the file specified by the string (strFilename) __init__(File)— opens the file specified by the file object __init__(fd)— opens the file specified by the file descriptor
__init__(strFilename)— opens the file specified by the string (strFilename) __init__(strFilename, bAppend)— same as above, but optionally opens the file in append mode __init__(File)— opens the file specified by the file object __init__(fd)— opens the file specified by the file descriptor
As an exercise, look up FileReader and FileWriter in the Java API documentation.