File Streams
The subclasses FileInputStream and FileOutputStream represent low-level streams that define byte input and output streams that are connected to files. Data can only be read or written as a sequence of bytes. Such file streams are typically used for handling image data.
A FileInputStream for reading bytes can be created using the following constructor:
FileInputStream(String name) throws FileNotFoundException
The file designated by the file name is assigned to a new file input stream.
If the file does not exist, a FileNotFoundException is thrown. If it exists, it is set to be read from the beginning. A SecurityException is thrown if the file does not have read access.
A FileOutputStream for writing bytes can be created using the following constructor:
FileOutputStream(String name) throws FileNotFoundException
FileOutputStream(String name, boolean append) throws FileNotFoundException
The file designated by the file name is assigned to a new file output stream.
If the file does not exist, it is created. If it exists, its contents are reset, unless the appropriate constructor is used to indicate that output should be appended to the file. A SecurityException is thrown if the file does not have write access or it cannot be created. A FileNotFoundException is thrown if it is not possible to open the file for any other reasons.
The FileInputStream class provides an implementation for the read() methods in its superclass InputStream. Similarly, the FileOutputStream class provides an implementation for the write() methods in its superclass OutputStream.
Example 20.1 demonstrates using a buffer to read bytes from and write bytes to file streams. The input and the output file names are specified on the command line. The streams are created at (1) and (2).
The bytes are read into a buffer by the read() method that returns the number of bytes read. The same number of bytes from the buffer are written to the output file by the write() method, regardless of whether the buffer is full or not after every read operation.
The end of file is reached when the read() method returns the value -1. The code at (3a) using a buffer can be replaced by a call to the transferTo() method at (3b) to do the same operation. The streams are closed by the try-with-resources statement. Note that most of the code consists of a try-with-resources statement with catch clauses to handle the various exceptions.
Example 20.1 Copying a File Using a Byte Buffer
/* Copy a file using a byte buffer.
Command syntax: java CopyFile <from_file> <to_file> */
import java.io.*;
class CopyFile {
public static void main(String[] args) {
try (// Assign the files:
FileInputStream fromFile = new FileInputStream(args[0]); // (1)
FileOutputStream toFile = new FileOutputStream(args[1])) { // (2)
// Copy bytes using buffer: // (3a)
byte[] buffer = new byte[1024];
int length = 0;
while((length = fromFile.read(buffer)) != -1) {
toFile.write(buffer, 0, length);
}
// Transfer bytes:
// fromFile.transferTo(toFile); // (3b)
} catch(ArrayIndexOutOfBoundsException e) {
System.err.println(“Usage: java CopyFile <from_file> <to_file>”);
} catch(FileNotFoundException e) {
System.err.println(“File could not be copied: ” + e);
} catch(IOException e) {
System.err.println(“I/O error.”);
}
}
}