Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Streams

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 and Character Streams

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

The Character Stream Classes


Character streams are defined by using two class hierarchies. At the top are two abstract classes, Reader and Writer. These abstract classes handle Unicode character Streams. Two of the most important methods are read ( ) and write ( ), which read and write characters of data, respectively

The PrintWriter Class


PrintWriter is one of the character-based classes. It defines several constructors. The one we will use is shown here: PrintWriter (OutputStream outputStream, boolean flushOnNewline) Here, outputStream is an object of type OutputStream, and flushOnNewline controls whether Java flushes the output stream every time a println ( ) method is called. If flushOnNewline is true, flushing automatically takes place. If false, flushing is not automatic PrintWriter supports the print ( ) and println ( ) methods for all types including Object. Thus, you can use these methods in the same way as they have been used with System.out. If an argument is not a simple type, the PrintWriter methods call the objects toString ( ) method and then print the result

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);

FILES and FILE HANDLING


File
Although most of the classes defined by java.io operate on streams, the File class does not. It deals directly with files and the file system. That is, the File class does not specify how information is retrieved from or stored in files; it describes the properties of a file itself. A File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies. Files are a primary source and destination for data within many programs. Although there are severe restrictions on their use within applets for security reasons, files are still a central resource for storing persistent and shared information. A directory in Java is treated simply as a File with one additional propertya list of filenames that can be examined by the list ( ) method. The following constructors can be used to create File objects: File (String directoryPath) File (String directoryPath, String filename) File (File dirObj, String filename) File (URI uriObj) Here, directoryPath is the path name of the file, filename is the name of the file, dirObj is a File object that specifies a directory, and uriObj is a URI object that describes a file. The fourth constructor was added by Java 2, version 1.4.The following example creates three files: f1, f2, and f3. The first File object is constructed with a directory path as the only argument. The second includes two argumentsthe path and the filename. The third includes the file path assigned to f1 and a filename; f3 refers to the same file as f2. File f1 = new File ("/"); File f2 = new File ("/","autoexec.bat"); File f3 = new File (f1,"autoexec.bat"); File defines many methods that obtain the standard properties of a File object. For example, getName ( ) returns the name of the file, getParent ( ) returns the name of the parent directory, and exists ( ) returns true if the file exists, false if it does not. The File class, however, is not symmetrical. By this, we mean that there are many methods that allow you to examine the properties of a simple file object, but no corresponding function exists to change those attributes

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

Reading and Writing Files


Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files FileInputDemo.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 :

OutputStream class hierarchy (partial)

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 Writer 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

You might also like