Streams: The Byte Stream Classes Are
Streams: The Byte Stream Classes Are
A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system This means that an input stream can abstract many different kinds of input: from a disk file, a keyboard, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection
Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data Character streams provide a convenient means for handling input and output of characters. They use Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than byte streams
The Byte Stream Classes are two abstract classes: InputStream and
OutputStream which defines several key methods that the other stream classes implement. Two of the most important are read ( ) and write ( ), which, respectively, read and write bytes of data. Both methods are declared as abstract inside InputStream and OutputStream. They are overridden by derived stream classes
To write to the console by using a PrintWriter, specify System.out for the output stream and flush the stream after each newline. For example, this line of code creates a PrintWriter that is connected to console output: PrintWriter pw = new PrintWriter (System.out, true);
e.g. FiIeInfo.java Directories A directory is a File that contains a list of other files and directories. When you create a File object and it is a directory, the isDirectory ( ) method will return true. In this case, you can call list ( ) on that object to extract the list of other files and directories inside. It has two forms. The first is shown here: String [ ] list ( ) The list of files is returned in an array of String objects. DirList.java
FilenameFilter
To limit the number of files returned by the list ( ) method to include only those files that match a certain filename pattern, or filter String [ ] list (FilenameFilter FFObj) In this form, FFObj is an object of a class that implements the FilenameFilter interface. FilenameFilter defines only a single method, accept ( ), which is called once for each file in a list. Its general form is given here: boolean accept (File directory, String filename) The accept ( ) method returns true for files in the directory specified by directory that should be included in the list (that is, those that match the filename argument), and returns false for those files that should be excluded. DirListOnly.java
java.io package Classes related to input and output are present in the JavaTM language package java.io . Java technology uses "streams" as a general mechanism of handling data. Input streams act as a source of data. Output streams act as a destination of data. File class The file class is used to store the path and name of a directory or file. The file object can be used to create, rename, or delete the file or directory it represents. The File class has the following constructors File(String pathname); // pathname could be file or a directory name File(String dirPathname, String filename); File(File directory, String filename);
The File class provides the getName() method which returns the name of the file excluding the directory name.
String getName();
Byte Streams The package java.io provides two set of class hierarchies - one for handling reading and writing of bytes, and another for handling reading and writing of characters. The abstract classes InputStream and OutputStream are the root of inheritance hierarchies handling reading and writing of bytes respectively. Figure : InputStream class hierarchy (partial)
Figure :
read and write methods InputStream class defines the following methods for reading bytes int read() throws IOException int read(byte b[]) throws IOException int read(byte b[], int offset, int length) throws IOException
Subclasses of InputStream implement the above mentioned methods. OutputStream class defines the following methods for writing bytes void write(int b) throws IOException void write(byte b[]) throws IOException void write(byte b[], int offset, int length) throws IOException
Subclasses of OutputStream implement the above mentioned methods. The example below illustrates code to read a character.
//First create an object of type FileInputStream type using the name of the file. FileInputStream inp = new FileInputStream("filename.ext"); //Create an object of type DataInputStream using inp. DataInputStream dataInp = new DataInputStream(inp); int i = dataInp.readInt();
Reader and Writer classes Similar to the InputStream and OutputStream class hierarchies for reading and writing bytes, Java technology provides class hierarchies rooted at Reader and Writer classes for reading and writing characters. A character encoding is a scheme for internal representation of characters. Java programs use 16 bit Unicode character encoding to represent characters internally. Other platforms may use a different character set (for example ASCII) to represent characters. The reader classes support conversions of Unicode characters to internal character shortage. Every platform has a default character encoding. Besides using default encoding, Reader and Writer classes can also
specify which encoding scheme to use. The Reader class hierarchy is illustrated below.
The table below gives a brief overview of key Reader classes. CharArrayReader The class supports reading of characters from a character array. The class supports reading of characters from a byte input InputStreamReader stream. A character encoding may also be specified. The class supports reading of characters from a file using FileReader default character encoding. The table below gives a brief overview of key Writer classes. The class supports writing of characters from a character array. The class supports writing of characters from a byte output OutputStreamReader stream. A character encoding may also be specified. The class supports writing of characters from a file using FileWriter default character encoding. CharArrayWriter
The example below illustrates reading of characters using the FileReader class.
//Create a FileReader class from the file name. FileReader fr = new FileReader ("filename.txt"); int i = fr.read(); //Read a character