Objectives The File Class

J.E.D.I. 12 Advanced IO Streams

12.1 Objectives

In a previous module, youve learned how to get user input and manipulate files using streams. You will now learn more about streams and other stream classes. After completing this lesson, you should be able to: 1. Enumerate the general stream types 2. Use the File class and its methods • Character and Byte Streams • Input and Output Streams • Node and Filter Streams 3. Use the different InputOutput classes • Reader • Writer • InputStream • OutputStream 4. Explain the concept of stream chaining 5. Define serialization 6. Explain the use of the transient keyword 7. Write and read from an object stream

12.2 General Stream Types

12.2.1 Character and Byte Streams

As mentioned before, there are generally two types of streams, the character and byte streams. Let us just review the basic difference between the two. Byte streams are file or device abstractions for binary data while character streams are for Unicode characters. The InputStream class is the abstract root class of all input byte streams whereas the OutputStream class is the abstract root class of all output byte streams. For character streams, the corresponding superclass of all classes are the Reader and the Writer class, respectively. Both classes are abstract classes for reading and writing to character streams.

12.2.2 Input and Output Streams

Streams are also categorized on whether they are used for reading or writing to streams. Although it is already quite obvious, allow me to define these types of streams. You are allowed to read from input streams but not write to them. On the other hand, you are allowed to write to output streams but not read from them. The InputStream class and the Reader class are the superclasses of all input streams. The OutputStream class and the Writer class are the root classes of all output streams. Input streams are also known as source streams since we get information from these Introduction to Programming II Page 158 J.E.D.I. streams. Meanwhile, output streams are also called sink streams.

12.2.3 Node and Filter Streams

Now, the java.io package distinguishes between node and filter streams. A node stream is a stream with the basic functionality of reading or writing from a specific location such as a disk or from the network. Types of node streams include files, memory and pipes. Filter streams, on the other hand, are layered onto node streams between threads or processes to provide additional functionalities not found in the node stream by themselves. Adding layers to a node stream is called stream chaining. The succeeding sections gives an overview of the different stream classes. For a complete list of these classes, please refer to Javas API documentation. Introduction to Programming II Page 159 J.E.D.I.

12.3 The File Class

Although the File class is not a stream class, it is important that we study it since the stream classes are manipulate files. The class is an abstract representation of actual files and directory pathnames. To instantiate a File object, you can use the following constructor: A File Constructor FileString pathname Instantiates a File object with the specified pathname as its filename. The filename may either be absolute i.e., containes the complete path or may consists of the filename itself and is assumed to be contained in the current directory. Table 43: File constructor The File class provides several methods for manipulating files and directories. Here are some of these methods. File Methods public String getName Returns the filename or the directory name of this File object. public boolean exists Tests if a file or a directory exists. public long length Returns the size of the file. public long lastModified Returns the date in milliseconds when the file was last modified. public boolean canRead Returns true if its permissible to read from the file. Otherwise, it returns false. public boolean canWrite Returns true if its permissible to write to the file. Otherwise, it returns false. public boolean isFile Tests if this object is a file, that is, our normal perception of what a file is not a directory. public boolean isDirectory Tests if this object is a directory. public String[] list Returns the list of files and subdirectories within this object. This object should be a directory. public void mkdir Creates a directory denoted by this abstract pathname. public void delete Introduction to Programming II Page 160 J.E.D.I. File Methods Removes the actual file or directory represented by this File object. Table 44: File methods Lets see how these methods work by trying out the following example: import java.io.; public class FileInfoClass { public static void mainString args[] { String fileName = args[0]; File fn = new FilefileName; System.out.printlnName: + fn.getName; if fn.exists { System.out.printlnfileName + does not exists.; Create a temporary directory instead. System.out.printlnCreating temp directory...; fileName = temp; fn = new FilefileName; fn.mkdir; System.out.printlnfileName + fn.exists? exists: does not exist; System.out.printlnDeleting temp directory...; fn.delete; System.out.printlnfileName + fn.exists? exists: does not exist; return; } System.out.printlnfileName + is a + fn.isFile? file. :directory.; if fn.isDirectory { String content[] = fn.list; System.out.printlnThe content of this directory:; for int i = 0; i content.length; i++ { System.out.printlncontent[i]; } } if fn.canRead { System.out.printlnfileName + is not readable.; return; } System.out.printlnfileName + is + fn.length + bytes long.; System.out.printlnfileName + was last modified on + fn.lastModified + .; if fn.canWrite { System.out.printlnfileName + is not writable.; } } } Here is a sample run of the FileInfoClass: Name: temp.txt temp.txt is a file. temp.txt is 34 bytes long. temp.txt was last modified on 1149150489177. temp.txt has contains the following text: Introduction to Programming II Page 161 J.E.D.I. what a wonderful world 1, 2, step

12.4 Reader Classes