Java Tutorial - Java Script : File Input Streams

Java Tutorial - Java Script :

File Input Streams

A file input stream can be created with the FileInputStream(String) constructor. The String argument should be the name of the file. You can include a path reference with the filename, which enables the file to be in a different folder from the class loading it. The following statement creates a file input stream from the file scores.dat:
FileInputStream fis = new FileInputStream(“scores.dat”);
Path references can be indicated in a manner specific to a platform, such as this example to read a file on a Windows system:
FileInputStream f1 = new FileInputStream(“\\data\\calendar.txt”);
Here’s a Linux example: FileInputStream f2 = new FileInputStream(“/data/calendar.txt”);  A better way to refer to paths is to use the class variable separator in the File class, which works on any operating system:char sep = File.separator; FileInputStream f2 = new FileInputStream(sep + “data” + sep + “calendar.txt”); After you create a file input stream, you can read bytes from the stream by calling its read() method. This method returns an integer containing the next byte in the stream. If the method returns –1, which is not a possible byte value, this signifies that the end of the file stream has been reached. To read more than one byte of data from the stream, call its read(byte[], int, int) method. The arguments to this method are as follows:
1. A byte array where the data will be stored
2. The element inside the array where the data’s first byte should be stored
3. The number of bytes to read
Unlike the other read() method, this does not return data from the stream. Instead, it returns either an integer that represents the number of bytes read or –1 if no bytes were read before the end of the stream was reached. The following statements use a while loop to read the data in a FileInputStream object called diskfile:
int newByte = 0;
while (newByte != -1) {
newByte = diskfile.read();
System.out.print(newByte + “ “);
}
Byte Streams 409
This loop reads the entire file referenced by diskfile one byte at a time and displays each byte, followed by a space character. It also displays –1 when the end of the files reached; you could guard against this easily with an if statement.The ByteReader application in Listing 15.1 uses a similar technique to read a file input stream. The input stream’s close() method is used to close the stream after the last byte in the file is read. Always close streams when you no longer need them; it frees system resources.
If you run this program, you’ll get the following error message: Error — java.io.FileNotFoundException: class.dat (The system cannot find the file specified). This error message looks like the kind of exception generated by the compiler, but it’s actually coming from the catch block in lines 20–22 of the ByteReader application. The exception is being thrown by lines 6–7 because the class.dat file cannot be found. You need a file of bytes in which to read. This can be any file, though files larger than a few megabytes in size will take a while to finish running. A suitable choice is the program’s class file, which contains the bytecode instructions executed by the Java virtual machine. Create this file by making a copy of ByteReader.class and renaming the copy class.dat. Don’t rename the ByteReader.class file or you won’t be able to run the program.
When you run the program, each byte in class.dat is displayed, followed by a count of the total number of bytes. If you used ByteReader.class to create class.dat, the last several lines of output should resemble the following:
12 0 50 0 13 0 56 0 14 0 61 0 16 0 64 0 17 0 67 0 18 0 71 0 19 0 96 0 22 0 99 0 20 0 100 0 21 0 128 0 23 0 28 0 0 0 32 0 6 254 0 14 7 0 29 1 1 252 0 46 1 250 0 2 2 255 0 31 0 1 7 0 30 0 1 7 0 31 28 0 1 0 32 0 0 0 2 0 33 -1
Bytes read: 1047
The number of bytes displayed on each line of output depends on the column width that text can occupy on your system. The bytes shown depend on the file used to create class.dat.