Input/Output in Java
A Java stream representing a sequence of data of a specific type
A Java I/O Stream represents a sequence of data of a specific type (bytes, characters, objects, and so on). I/O stands for Input/Output. A stream can get its input from a variety of sources (standard input through a console, file input etc) and send output to a variety of destinations ( a console, a file, a printer, a web browser etc.). Streams are used to read or write data one unit at a time. The java.io package contains a variety of input stream readers and output stream writers that may be used to read from and write to files, objects, Java primitive types and so on. A lot of overhead due to disk access, network and file access and so on may be avoided by using Buffered input/output streams. Such streams read data from, or write data to a memory area known as a buffer. The size of the buffer is specified beforehand. Overheads caused by communication with the native OS are reduced if buffers are used; and application to OS communication happens only when the buffer is empty (for read/input - the buffer is loaded from the input stream at this point) or when the buffer is full (for write/output - the buffer is written to the output stream and emptied at this point). Take a look at the documentation of the java.io package for more information about Stream readers. While byte streams deal with raw bytes or binary data, character streams deal with character data.
A File object in Java (java.io.file) may be set to refer to a file on the System:
File fruitImage = new File("apple.jpeg");
Note that the File object simply contains a reference to the file and not the file itself. Several methods may be utilized to check if the File object exists, the file's full path, permissions, time stamp, and so on. The File object may also be used to delete a file or rename it. A RandomAccessFile class from java.io allows developers to jump to parts of a file without going through it sequentially. The pointer jumps to an offset specified as the input parameter for the seek method of the RandomAccessFile class.
The createFruits.java file uses a parameter passed into its main method (args[0]) - the path and name of a text file that contains the names and other properties of several fruits. This parameter should be typed next to the name of the class when the application is initiated: Java createFruits c:\mypath\myfruits.txt.
The very first line in the file contains the path to the fruit image files. After this initial line, each line of the input file will contain the properties of a single fruit in a semicolon-separated format, wherein the name comes first, followed by a ';'. A comma-separated list of colors terminated with a ';' should come next, followed by the name of the image file and finally the number of calories. This input file is accessed and each line is read using the class java.io.BufferedReader. The following import statements make File I/O related classes used by createFruits.java available at link time.
import java.io.BufferedReader; import java.io.FileReader; import java.io.File;
The input file is loaded into the BufferedReader class from the java.io package so that the program can access its contents. The BufferedReader is used to save OS call-related overhead. The input file itself is opened using a FileReader (a File Object cannot be directly accessed using a BufferedReader). This FileReader is used as the argument to construct a BufferedReader variable named 'fileInput'. The string returned by the readline method contains null when the end of the file is reached; this condition is used to end a 'while' loop that iterates through each line of text in the file. The try-catch section handles exceptions thrown when the java.io.BufferedReader and java.io.FileReader classes try to access a file that does not exist or cannot be accessed due to IO related problems.
BufferedReader fileInput;
.
.
.
.
try {
fileInput = new BufferedReader(new FileReader(new File(path))) ;
imagePath = fileInput.readLine();
while ((lineFromFile = fileInput.readLine()) != null) {
fruitProperties = lineFromFile.split(";");
.
.
Java Code
.
.
}
}
catch (java.io.FileNotFoundException fnf) {
System.out.println("Input File not Found: " + fnf.getMessage());
}
catch (java.io.IOException ioe) {
System.out.println("Problems Reading File: " + ioe.getMessage());
}
The java.io package is also used to test if the image file exists in the path specified in the first line of the input text file. The first line read from the input file contains the path to the images and the variable imagePath is set to this line. Each subsequent line in the input file contains the name of the image file as the third item in a semicolon delimited list. This line is split into an Array of Strings and the third array element (fruitProperties[2]) contains the image file name
A new File object that references the name of each image file is created within the while loop that iterates through each line in the input file. The fruit object's image property is set to contain the name of the file only if the file exists; otherwise the image property is set to contain 'No image'. This is achieved by passing the image name to a four argument constructor if the image file exists and calling a three argument constructor (this constructor in Fruit.java sets the image property to 'no image') without the filename if the image file does not exist.
imagePath = fileInput.readLine();
.
.
File imageFile = new File(imagePath + "\\" + fruitProperties[2]);
if (imageFile.exists()) {
myFruits.add(new Fruit(fruitProperties[0],
Arrays.asList(fruitProperties[1].split(",")),
fruitProperties[2],
java.lang.Integer.parseInt(fruitProperties[3])));
}
else {
myFruits.add(new Fruit(fruitProperties[0],
Arrays.asList(fruitProperties[1].split(",")),
java.lang.Integer.parseInt(fruitProperties[3])));
}