There are four types of streams: InputStream, OutputStream, Reader, and Writer.
Reader. A stream to read characters.
Writer. A stream to write characters.
InputStream. A stream to read binary data.
OutputStream. A stream to write binary data.
For better performance, use the buffered I/O classes: BufferedInputStream, BufferedOutputStream, BufferedReader, and BufferedWriter.
Reading from and writing to a stream dictate that you do so sequentially. The java.io.RandomAccessFile is for non-sequential operations.
For object serialization and deserialization, you can use the ObjectInputStream and ObjectOutputStream classes.
You use an InputStream to read binary data.
InputStream
|
+-- FileInputStream
|
+-- BufferedInputStream
FileInputStream enables easy reading from a file.
BufferedInputStream provides data buffering that improves performance.
The ObjectInputStream class is used in object serialization.
The OutputStream abstract class represents a stream for writing binary data
OutputStream
|
+--FileOutputStream
|
+--BufferedOutputStream.
|
+--ObjectOutputStream
FileOutputStream provides a convenient way to write to a file.
BufferedOutputStream provides better performance.
ObjectOutputStream class plays an important role in object serialization.
The OutputStream class defines three write method overloads, which are mirrors of the read method overloads in InputStream:
public void write (int b)
public void write (byte[] data)
public void write (byte[] data, int offset, int length)
The first overload writes the lowest 8 bits of the integer b to this OutputStream.
The second writes the content of a byte array to this OutputStream.
The third overload writes length bytes of the data starting at offset offset.
close() method closes the OutputStream.
flush() method forces any buffered content to be written out to the sink
A File object represents a file or directory pathname. You can pass an absolute path to a file or directory like this:
File file1 = new File ("C:\\temp\\myNote.txt"); // in Windows
File file2 = new File ("/tmp/myNote.txt"); // in Linux/Unix
getName(): Returns the name of the file without the path or the directory name
The java.io.File class in Mustang comes with several new features.
The java.io.File class in Mustang provides methods for checking hard disk space and the amount used.
The java.io.File class in Mustang possesses methods for changing file attributes.
Here are the new methods:
public long getFreeSpace () Returns the size, in bytes, of the partition referenced by this File object.
public long getFreeSpace () Returns the amount of free space, in bytes, in the partition referenced by this File object.
public long getUsableSpace () Returns the number of bytes available to this virtual machine on the partition referenced by this File object. The difference between getUsableSpace and getFreeSpace is that the former takes into account restrictions imposed by the operating system, such as write permissions. The latter does not.
public boolean setWritable (boolean writable, boolean ownerOnly) Sets the owner's or everybody's write permission for the path referenced by this File object.
public boolean setWritable (boolean writable) Sets the owner's write permission for the path referenced by this File object.
public boolean setReadable (boolean readable, boolean ownerOnly) Sets the owner's or everybody's read permission for the path referenced by this File object.
public boolean setReadable (boolean readable) Sets the owner's read permission for the path referenced by this File object.
public boolean setExecutable (boolean executable, boolean ownerOnly) Sets the owner's or everybody's execute permission for the path referenced by this File object.
public boolean setExecutable (boolean executable) Sets the owner's execute permission for the path referenced by this File object.
public boolean canExecute () Tests if the application has the right to execute the file referenced by this File object.
Java 6 adds canExecute plus setReadable, setWritable, and setExecutable methods to change a file’s attributes.