It would be impossible for a program to do anything useful without performing some kind of input or output of data. Most programs require input from the user and in return output information to the screen, printer, and often to files. The Java I/O package provides an extensive set of classes for handling input and output to and from many different devices. In this chapter you learn about the primary classes contained in the I/O package, along with some examples that show off the capabilities of these classes.
The I/O package, which is also known as java.io, contains many classes, each with a variety of member variables and methods. This chapter does not take an exhaustive look at every class and method contained in the I/O package. Instead, you may view this chapter as a tutorial on how to perform basic input and output using the more popular I/O classes. Armed with the information learned in this chapter, you will be ready to begin using the Java I/O classes in your own programs. And should you choose to explore the more complex I/O classes supported by Java, you will be prepared for the challenge.
The Java input model is based on the concept of an input stream. An input stream can be thought of much like a physical (and certainly more literal) stream of water flowing from a water plant into the pipes of a water system. The obvious difference is that an input stream deals with binary computer data rather than physical water. The comparison is relevant, however, because the data going into an input stream flows like the water being pumped into a pipe. Data that is pumped into an input stream can be directed in many different ways, much like water is directed through the complex system of pipes that make up a water system. The data in an input stream is transmitted a byte at a time, which is roughly analogous to individual drops of water flowing into a pipe.
More practically speaking, Java uses input streams as the means of reading data from an input source, such as the keyboard. The basic input stream classes supported by Java follow:
The InputStream class is an abstract class that serves as the base class for all the other input stream classes. InputStream defines a basic interface for reading streamed bytes of information. The methods defined by the InputStream class will become very familiar to you because they serve a similar purpose in every InputStream derived class. This design approach enables you to learn the protocol for managing input streams once, and then apply it to different devices using an InputStream derived class.
The typical scenario when using an input stream is to create an InputStream derived object and tell it you want to input information (by calling an appropriate method). If no input information is currently available, the InputStream uses a technique known as blocking to wait until input data becomes available. An example of when blocking will take place is the case of using an input stream to read information from the keyboard. Until the user has typed in information and pressed Return, there is no input available to the InputStream object. The InputStream object then waits (blocks) until the user presses Return, in which case the input data becomes available and the InputStream object can process it as input.
The InputStream class defines the following methods:
InputStream defines three different read methods for reading input data in various ways. The first read method takes no parameters and simply reads a byte of data from the input stream and returns it as an integer. This version of read returns -1 if the end of the input stream is reached. Because this version of read returns a byte of input as an int, you will need to cast it to a char if you are reading characters. The second version of read takes an array of bytes as its only parameter, enabling you to read multiple bytes of data at once. You have to make sure that the byte array passed into read is large enough to hold the information being read, or an IOException will be thrown. This version of read returns the actual number of bytes read, or -1 if the end of the stream is reached. The last version of read takes a byte array, an integer offset, and an integer length as parameters. This version of read is very similar to the second version, except it enables you to specify where in the byte array you want the new information placed. The off parameter specifies the offset into the byte array to start placing new data, and the len parameter specifies the maximum number of bytes to read.
The skip method is used to skip over bytes of data in the input stream. skip takes a long value n as its only parameter, which specifies how many bytes of input to skip. It returns the actual number of bytes skipped, or -1 if the end of the input stream is reached.
The available method is used to determine the number of bytes of input data that can be read without blocking. available takes no parameters and returns the number of available bytes. This method is useful if you want to ensure that there is input data available so as to avoid the blocking mechanism.
The mark method marks the current position in the stream. This position can later be returned to using the reset method. The mark and reset methods are useful in situations where you want to read ahead in the stream but not lose your original position. An example of this situation is verifying a file type, such as an image file. You would probably read the file header first and mark the position at the end of the header. You would then read some of the data to make sure it follows the format expected for that file type. If the data doesnt look right, you can reset the read pointer and try a different technique.
Notice that the mark method takes an integer parameter, readlimit. readlimit specifies how many bytes can be read before the mark becomes invalidated. In effect, readlimit determines how far you can read ahead and still be able to reset the marked position. The markSupported method returns a Boolean value representing whether or not an input stream supports the mark/reset functionality.
Finally, the close method closes an input stream and releases any resources associated with the stream. It is not necessary to explicitly call close, since input streams are automatically closed when the InputStream object is destroyed. Although it is not necessary, calling close immediately after you are finished using a stream is a good programming practice. The reason for this is that close causes the stream buffer to be flushed, which helps avoid file corruption.
The keyboard is the most standard input device for retrieving user input. The System class contained in the language package contains a member variable that represents the keyboard, or standard input stream. This member variable is called in and is an instance of the InputStream class. This variable is useful for reading user input from the keyboard. Listing 20.1 contains the ReadKeys1 program, which shows how the System.in object can be used along with the first version of the read method. This program can be found on the CD-ROM in the file ReadKeys1.java.
NOTE |
---|
I mentioned the keyboard as being the standard input stream. This isnt totally true, because the standard input stream can receive input from any number of sources. Although the keyboard certainly is the most common method of feeding input to the standard input stream, it is not the only method. An example of the standard input stream being driven by a different input source is the redirection of a file into a stream. |
class ReadKeys1 { public static void main (String args[]) { StringBuffer s = new StringBuffer(); char c; try { while ((c = (char)System.in.read()) != \n) { s.append(c); } } catch (Exception e) { System.out.println(Error: + e.toString()); } System.out.println(s); } }
The ReadKeys1 class first creates a StringBuffer object called str. It then enters a while loop that repeatedly calls the read method until a newline character is detected (the user hits Return). Notice that the input data returned by read is cast to a char type before being stored in the character variable c. Each time a character is read, it is appended to the string buffer using the append method of StringBuffer. It is important to see how any errors caused by the read method are handled by the try/catch exception handling blocks. The catch block simply prints an error message to the standard output stream based on what error occurred. Finally, when a newline character is read from the input stream, the println method of the standard output stream is called to output the string to the screen. Youll learn more about the standard output stream a little later in this chapter.
Listing 20.2 contains ReadKeys2, which is similar to ReadKeys1 except that it uses the second version of the read method. This read method takes an array of bytes as a parameter to store the input that is read. ReadKeys2 can be found on the CD-ROM in the file ReadKeys2.java.
class ReadKeys2 { public static void main (String args[]) { byte buf[] = new byte[80]; try { System.in.read(buf); } catch (Exception e) { System.out.println(Error: + e.toString()); } String s = new String(buf, 0); System.out.println(s); } }
In ReadKeys2, an array of bytes is created that is 80 bytes long. A single read method call is performed that reads everything the user has typed. The input is blocked until the user presses Return, in which case the input becomes available and the read method fills the byte array with the new data. A String object is then created to hold the constant string previously read. Notice that the creation method, or constructor, used to create the String object takes an array of bytes (buf) as the first parameter and appends the high byte value specified in the second parameter to each byte, thus forming 16-bit Unicode characters. Because the standard ASCII characters map to Unicode characters with zeros in the high byte, passing 0 as the high byte to the constructor works perfectly. Finally, println is again used to output the string.
The ReadKeys3 program in Listing 20.3 shows how to use the last version of the read method. This version of read again takes an array of bytes, as well as an offset and length for determining how to store the input data in the byte array. ReadKeys3 can be found on the CD-ROM in the file ReadKeys3.java.
class ReadKeys3 { public static void main (String args[]) { byte buf[] = new byte[10]; try { System.in.read(buf, 0, 10); } catch (Exception e) { System.out.println(Error: + e.toString()); } String s = new String(buf, 0); System.out.println(s); } }
ReadKeys3 is very similar to ReadKeys2, with one major difference: the third version of the read method is used to limit the maximum number of bytes read into the array. The size of the byte array is also shortened to ten bytes to show how this version of read handles it when more data is available than the array can hold. Remember that this version of read can also be used to read data into a specific offset of the array. In this case, the offset is specified as 0 so that the only difference is the maximum number of bytes that can be read (10). This is a useful technique of guaranteeing that you dont overrun a byte array.
The BufferedInputStream class, as its name implies, provides a buffered stream of input. This means that more data is read into the buffered stream than you might have requested, meaning that subsequent reads come straight out of the buffer, rather than the input device. This can result in much faster read access, because reading from a buffer is really just reading from memory. BufferedInputStream implements all of the same methods defined by InputStream. As a matter of fact, it doesnt implement any new methods of its own. However, the BufferedInputStream class does have two different constructors, which follow:
Notice that both constructors take an InputStream object as the first parameter. The only difference between the two is the size of the internal buffer. In the first constructor, a default buffer size is used, whereas in the second constructor you specify the buffer size with the size integer parameter. To support buffered input, the BufferedInputStream class also defines a handful of member variables, which follow:
The buf byte array member is the buffer where input data is actually stored. The count member variable keeps up with how many bytes are stored in the buffer. The pos member variable keeps up with the current read position in the buffer. The markpos member variable specifies the current mark position in the buffer as set using the mark method. markpos is equal to -1 if no mark has been set. And finally, the marklimit member variable specifies the maximum number of bytes that can be read before the mark position is no longer valid. marklimit is set by the readlimit parameter passed into the mark method. All these member variables are specified as protected, so you will probably never actually use any of them. However, seeing these variables should give you some insight into how the BufferedInputStream class implements the methods defined by InputStream.
Listing 20.4 contains the ReadKeys4 program, which uses a BufferedInputStream object instead of System.in to read input from the keyboard. ReadKeys4 can be found in the file ReadKeys4.java on the CD-ROM.
import java.io.*; class ReadKeys4 { public static void main (String args[]) { BufferedInputStream in = new BufferedInputStream(System.in); byte buf[] = new byte[10]; try { in.read(buf, 0, 10); } catch (Exception e) { System.out.println(Error: + e.toString()); } String s = new String(buf, 0); System.out.println(s); } }
Notice first that the BufferedInputStream class must be imported from the I/O package. Actually, in this case the * qualifier is used to import all the classes in the I/O package. The BufferedInputStream object is created by passing the System.in InputStream into its constructor. From there on, the program is essentially the same as ReadKeys3, except that the read method is called on the BufferedInputStream object rather than System.in.
The DataInputStream class is useful for reading primitive Java data types from an input stream in a portable fashion. There is only one constructor for DataInputStream, which simply takes an InputStream object as its only parameter. This constructor is defined as follows:
DataInputStream(InputStream in)
DataInputStream implements the following useful methods beyond those defined by InputStream:
The skipBytes method works very similarly to skip, with the exception being that skipBytes blocks until all bytes are skipped. The number of bytes to skip is determined by the integer parameter n. There are two readFully methods implemented by DataInputStream. These methods are similar to the read methods, except that they block until all data has been read. The normal read methods only block until some data is available, not all. The readFully methods are to the read methods what skipBytes is to skip.
The readLine method is used to read a line of text that has been terminated with a newline (\n), carriage return (\r), carriage return/newline (\r\n), or end-of-file character sequence (EOF). readLine returns the line of text in a String object. Listing 20.5 contains the ReadFloat program, which uses the readLine method to read a floating point value from the user.
import java.io.*; class ReadFloat { public static void main (String args[]) { DataInputStream in = new DataInputStream(System.in); String s = new String(); try { s = in.readLine(); float f = Float.valueOf(s).floatValue(); System.out.println(f); } catch (Exception e) { System.out.println(Error: + e.toString()); } } }
In ReadFloat, a DataInputStream object is first created based on System.in. A String object is then created to hold the input line of text. The readLine method is called with the resulting line of text being stored in the String object s. A floating point number is extracted from the string by first getting a Float object from the string by using the valueOf static method of the Float class. The floatValue method is then called on the Float object to get a float value, which is then stored in the float variable f. This value is then output to the screen using println.
The rest of the methods implemented by DataInputStream are variations of the read method for different fundamental data types. The type read by each method is easily identifiable by the name of the method.
The FileInputStream class is useful for performing simple file input. For more advanced file input operations, you will more than likely want to use the RandomAccessFile class, which is discussed a little later in this chapter. The FileInputStream class can be instantiated using one of three following constructors:
The first constructor takes a String object parameter called name, which specifies the name of the file to use for input. The second constructor takes a File object parameter that specifies the file to use for input. Youll learn more about the File object near the end of this chapter. The third constructor for FileInputStream takes a FileDescriptor object as its only parameter.
The FileInputStream class functions exactly like the InputStream class, except that it is geared toward working with files. Listing 20.6 contains the ReadFile program, which uses the FileInputStream class to read data from a text file. ReadFile can be found in the file ReadFile.java on the CD-ROM.
import java.io.*; class ReadFile { public static void main (String args[]) { byte buf[] = new byte[64]; try { FileInputStream in = new FileInputStream(Grocery.txt); in.read(buf, 0, 64); } catch (Exception e) { System.out.println(Error: + e.toString()); } String s = new String(buf, 0); System.out.println(s); } }
In ReadFile, a FileInputStream object is first created by passing a string with the name of the file (Grocery.txt) as the input file. The read method is then called to read from the input file into a byte array. The byte array is then used to create a String object, which is in turn used for output. Pretty simple!
The StringBufferInputStream class, aside from having a very long name, is a pretty neat class. StringBufferInputString enables you to use a string as a buffered source of input. StringBufferInputStream implements all the same methods defined by InputStream, and no more. The StringBufferInputStream class has a single constructor, which follows:
StringBufferInputStream(String s)
The constructor takes a String object, which it constructs the string buffer input stream out of. Although StringBufferInputStream doesnt define any additional methods, it does provide a few of its own member variables, which follow:
The buffer string member is the buffer where the string data is actually stored. The count member variable specifies the number of characters to use in the buffer. Finally, the pos member variable keeps up with the current position in the buffer. Like the BufferedInputStream class, you will probably never see these member variables, but they are important in understanding how the StringBufferInputStream class is implemented.
Listing 20.7 contains the ReadString program, which uses a StringBufferInputStream to read data from a string of text data. ReadString can be found in the file ReadString.java on the CD-ROM.
import java.io.*; class ReadString { public static void main (String args[]) { // Get a string of input from the user byte buf1[] = new byte[64]; try { System.in.read(buf1, 0, 64); } catch (Exception e) { System.out.println(Error: + e.toString()); } String s1 = new String(buf1, 0); // Read the string as a string buffer and output it StringBufferInputStream in = new StringBufferInputStream(s1); byte buf2[] = new byte[64]; try { in.read(buf2, 0, 64); } catch (Exception e) { System.out.println(Error: + e.toString()); } String s2 = new String(buf2, 0); System.out.println(s2); } }
The ReadString program enables the user to type in text, which is read and stored in a string. This string is then used to create a StringBufferInputStream that is read into another string for output. Obviously, this program goes to a lot of trouble to do very little; its only meant as a demonstration of how to use the StringBufferInputStream class. Knowing this, its up to you to find an interesting application to apply this class to.
The first half of the ReadString program should look pretty familiar; its essentially the guts of the ReadKeys3 program, which reads data entered by the keyboard into a string. The second half of the program is where you actually get busy with the StringBufferInputStream object. A StringBufferInputStream object is created using the String object (s1) containing the text entered from the keyboard. The contents of the StringBufferInputStream object are then read into a byte array using the read method. The byte array is in turn used to construct another String object (s2), which is output to the screen.
In Java, output streams are the logical counterparts to input streams and handle writing data to output sources. Using the water analogy from the discussion of input streams earlier, an output stream would be equivalent to the water spout on your bathtub. Just as water travels from a water plant through the pipes and out of the spout into your bathtub, so must data flow from an input device through the operating system and out of an output device. A leaky water spout is an even better way to visualize the transfer of data out of an output stream; each drop of water falling out of the spout represents a byte of data. Each byte of data flows to the output device just like the drops of water falling one after the next out of the bathtub spout.
Getting back to Java, you use output streams to output data to various different output devices, such as the screen. The primary output stream classes used in Java programming follow:
The Java output streams provide a variety of ways to output data. The OutputStream class defines the core behavior required of an output stream. The PrintStream class is geared toward outputting text data, such as the data sent to the standard output stream. The BufferedOutputStream class is an extension to the OutputStream class that provides support for buffered output. The DataOutputStream class is useful for outputting primitive data types such as int or float. And finally, the FileOutputStream class provides the support necessary to output data to files.
The OutputStream class is the output counterpart to InputStream and serves as an abstract base class for all the other output stream classes. OutputStream defines the basic protocol for writing streamed data to an output device. Like the methods for InputStream, you will become accustomed to the methods defined by OutputStream, because they act very much the same in every OutputStream derived class. The benefit of this common interface is that you can essentially learn a method once and then be able to apply it to different classes without starting the learning process over again.
You will typically create an OutputStream derived object and call an appropriate method to tell it you want to output information. The OutputStream class uses a technique similar to the one used by InputStream; it will block until data has been written to an output device. While blocking (waiting for the current output to be processed), the OutputStream class will not allow any further data to be output.
The OutputStream class implements the following methods:
OutputStream defines three different write methods for writing data in a few different ways. The first write method writes a single byte to the output stream, as specified by the integer parameter b. The second version of write takes an array of bytes as a parameter and writes them to the output stream. The last version of write takes a byte array, an integer offset, and a length as parameters. This version of write is very much like the second version, except it uses the other parameters to determine where in the byte array to begin outputting data, along with how much data to output. The off parameter specifies an offset into the byte array to begin outputting data from, and the len parameter specifies how many bytes are to be output.
The flush method is used to flush the output stream. Calling flush will force the OutputStream object to output any pending data.
Finally, the close method closes an output stream and releases any resources associated with the stream. Like InputStream objects, it isnt usually necessary to call close on an OutputStream object, because streams are automatically closed when they are destroyed.
The PrintStream class is derived from OutputStream and is designed primarily for printing output data as text. PrintStream has two different constructors:
Both PrintStream constructors take an OutputStream object as their first parameter. The only difference between the two methods is how the newline character is handled. In the first constructor, the stream is flushed based on an internal decision by the object. In the second constructor, you can specify that the stream be flushed every time it encounters a newline character. You specify this through the Boolean autoflush parameter.
The PrintStream class also implements a rich set of methods, which follow:
The checkError method flushes the stream and returns whether or not an error has occurred. The return value of checkError is based on an error ever having occurred on the stream, meaning that once an error occurs, checkError will always return true for that stream.
PrintStream provides a variety of print methods to handle all your printing needs. The version of print that takes an Object parameter simply outputs the results of calling the toString method on the object. The other print methods each take a different type parameter that specifies which data type is printed for each.
The println methods implemented by PrintStream are very similar to the print methods. The only difference is that the println methods print a newline character following the data that is printed. The println method that takes no parameters simply prints a newline character by itself.
The monitor is the primary output device on modern computer systems. The System class has a member variable that represents the standard output stream, which is typically the monitor. The member variable is called out and is an instance of the PrintStream class. out is very useful for outputting text to the screen. But you already know this because youve seen the out member variable in most of the sample programs developed thus far.
The BufferedOutputStream class, which is very similar to the OutputStream class, provides a buffered stream of output. This enables you to write to a stream without causing a bunch of writes to an output device. The BufferedOutputStream class maintains a buffer that is written to when you write to the stream. When the buffer gets full or when it is explicitly flushed, it is written to the output device. This output approach is much more efficient since most of the data transfer is taking place in memory. And when it does come time to output the data to a device, it all happens at once.
The BufferedOutputStream class implements the same methods defined in OutputStream, meaning that there are no additional methods, except for constructors. The two constructors for BufferedOutputStream follow:
Both constructors for BufferedOutputStream take an OutputStream object as their first parameter. The only difference between the two is the size of the internal buffer used to store the output data. In the first constructor, a default buffer size is used, where in the second constructor you specify the buffer size with the size integer parameter. The buffer itself within the BufferedOutputStream class is managed by two member variables, which follow:
The buf byte array member variable is the actual data buffer where output data is stored. The count member keeps up with how many bytes are in the buffer. These two member variables are sufficient to represent the state of the output stream buffer.
Listing 20.8 contains the WriteStuff program, which uses a BufferedOutputStream object to output a byte array of text data. WriteStuff can be found in the file WriteStuff.java on the CD-ROM.
import java.io.*; class WriteStuff { public static void main (String args[]) { // Copy the string into a byte array String s = new String(Dance, spider!\n); byte[] buf = new byte[64]; s.getBytes(0, s.length(), buf, 0); // Output the byte array (buffered) BufferedOutputStream out = new BufferedOutputStream(System.out); try { out.write(buf, 0, 64); out.flush(); } catch (Exception e) { System.out.println(Error: + e.toString()); } } }
The WriteStuff program fills a byte array with text data from a string and outputs the byte array to the screen using a buffered output stream. WriteStuff begins by creating a String object containing text, and a byte array. The getBytes method of String is used to copy the bytes of data in the string to the byte array. The getBytes method copies the low byte of each character in the string to the byte array. This works because the Unicode representation of ASCII characters has zeros in the high byte. Once the byte array is ready, a BufferedOutputStream object is created by passing System.out into the constructor. The byte array is then written to the output buffer using the write method. Since the stream is buffered, it is necessary to call the flush method to actually output the data.
The DataOutputStream class is useful for writing primitive Java data types to an output stream in a portable way. DataOutputStream only has one constructor, which simply takes an OutputStream object as its only parameter. This constructor is defined as follows:
DataOutputStream(OutputStream out)
The DataOutputStream class implements the following useful methods beyond those inherited from OutputStream:
The size method is used to determine how many bytes have been written to the stream thus far. The integer value returned by size specifies the number of bytes written.
The rest of the methods implemented in DataOutputStream are all variations on the write method. Each different version of writeType takes a different data type that is in turn written as output.
The FileOutputStream class provides a means to perform simple file output. For more advanced file output, you should check out the RandomAccessFile class, which is discussed a little later in this chapter. A FileOutputStream object can be created using one of the three following constructors:
The first constructor takes a String parameter, which specifies the name of the file to use for input. The second constructor takes a File object parameter that specifies the input file. Youll learn about the File object a little later in this chapter. The third constructor takes a FileDescriptor object as its only parameter.
The FileOutputStream class functions exactly like the OutputStream class, except that it is specifically designed to work with files. Listing 20.9 contains the WriteFile program, which uses the FileOutputStream class to write user input to a text file. WriteFile can be found in the file WriteFile.java on the CD-ROM.
import java.io.*; class WriteFile { public static void main (String args[]) { // Read the user input byte buf[] = new byte[64]; try { System.in.read(buf, 0, 64); } catch (Exception e) { System.out.println(Error: + e.toString()); } // Output the data to a file try { FileOutputStream out = new FileOutputStream(Output.txt); out.write(buf); } catch (Exception e) { System.out.println(Error: + e.toString()); } } }
In WriteFile, user input is read from the standard input stream into a byte array using the read method of InputStream. A FileOutputStream object is then created with a filename of Output.txt 4", which is passed in as the only parameter to the constructor. The write method is then used to output the byte array to the stream. You can see that working with output file streams is just as easy as working with input file streams.
If the FileInputStream and FileOutputStream classes dont quite meet up to your file handling expectations, dont despair! Java provides two more classes for working with files that are sure to meet your needs. These two classes are File and RandomAccessFile. The File class basically models an operating system directory entry, enabling you access to information about a file including file attributes and the full path where the file is located, among other things. The RandomAccessFile class, on the other hand, provides a variety of methods for reading and writing data to and from a file.
The File class can be instantiated using one of three constructors, which follow:
The first constructor takes a single String parameter that specifies the full path name of the file. The second constructor takes two String parameters: path and name. The path parameter specifies the directory path where the file is located, while the name parameter specifies the name of the file. The third constructor is similar to the second, except it takes another File object as the first parameter instead of a string. The File object in this case is used to specify the directory path of the file.
The most important methods implemented by the File class follow:
The getName method gets the name of a file and returns it as a string. The getPath method returns the path of a file, which may be relative, as a string. The getAbsolutePath method returns the absolute path of a file. The getParent method returns the parent directory of a file, or null if a parent directory is not found.
The exists method returns a Boolean specifying whether or not a file actually exists. The canWrite and canRead methods return Boolean values the specify whether a file can be written to or read from. The isFile and isDirectory methods return Boolean values the specify if a file is valid and if the directory information is valid. The isAbsolute method returns a Boolean value specifying if a filename is absolute.
The lastModified method returns a long value that specifies the time in which a file was last modified. The long value returned is only useful in determining differences between modification times; it has no meaning as an absolute time and is not suitable for output. The length method returns the length of a file in bytes.
The mkdir method creates a directory based on the current path information. mkdir returns a Boolean indicating the success of creating the directory. The mkdirs method is similar to mkdir, except that it can be used to create an entire directory structure. The renameTo method renames a file to the name specified by the File object passed as the dest parameter. The delete method deletes a file. Both renameTo and delete return a Boolean value indicating success or failure.
Finally, the list methods of the File object obtain listings of the directory contents. Both list methods return a list of filenames in a String array. The only difference between the two is that the second version takes a FilenameFilter object that enables you to filter out certain files from the list.
Listing 20.10 contains the source code for the FileInfo program, which uses a File object to determine information about a file in the current directory. The FileInfo program is located in the FileInfo.java source file on the CD-ROM.
import java.io.*; class FileInfo { public static void main (String args[]) { System.out.println(Enter file name: ); char c; StringBuffer buf = new StringBuffer(); try { while ((c = (char)System.in.read()) != \n) buf.append(c); } catch (Exception e) { System.out.println(Error: + e.toString()); } File file = new File(buf.toString()); if (file.exists()) { System.out.println(File Name : + file.getName()); System.out.println( Path : + file.getPath()); System.out.println(Abs. Path : + file.getAbsolutePath()); System.out.println(Writable : + file.canWrite()); System.out.println(Readable : + file.canRead()); System.out.println(Length : + (file.length() / 1024) + KB); } else System.out.println(Sorry, file not found.); } }
The FileInfo program uses the File object to get information about a file in the current directory. The user is first prompted to type in a filename, with the resulting input being stored in a String object. The String object is then used as the parameter to the File objects constructor. A call to the exists method determines if the file actually exists. If so, information about the file is obtained through the various File methods and the results output to the screen.
The RandomAccessFile class provides a multitude of methods for reading and writing to files. Although you can certainly use FileInputStream and FileOutputStream for file I/O, RandomAccessFile provides many more features and options. Following are the constructors for RandomAccessFile:
The first constructor takes a String parameter specifying the name of the file to access, along with a String parameter specifying the type of mode (read or write). The mode type can be either r for read mode, or rw for read/write mode. The second constructor takes a File object as the first parameter, which specifies the file to access. The second parameter is a mode string, which works exactly the same as in the first constructor.
The RandomAccessFile class implements a variety of powerful file I/O methods. Following are some of the most useful ones:
From looking at this method list, you no doubt are thinking that many of these methods look familiar. And they should look familiar; most of the methods implemented by RandomAccessFile are also implemented by either FileInputStream or FileOutputStream. The fact that RandomAccessFile combines them into a single class is a convenience in and of itself. But you already know how to use these methods because they work just like they do in FileInputStream and FileOutputStream. What you are interested in are the new methods implemented by RandomAccessFile.
The first new method you might have noticed is the getFilePointer method. getFilePointer returns the current position of the file pointer as a long value. The file pointer indicates the location in the file where data will be read from or written to next. In read mode, the file pointer is analogous to the needle on a phonograph or the laser in a CD player. The seek method is the other new method that should catch your attention. seek sets the file pointer to the absolute position specified by the long parameter pos. Calling seek to move the file pointer is analogous to moving the phonograph needle with your hand. In both cases, the read point of the data or music is being moved. It is a similar situation when you are writing data as well.
Listing 20.11 contains the source code for FilePrint, which is a program that uses the RandomAccessFile class to print a file to the screen. The source code for the FilePrint program can be found in the file FilePrint.java on the CD-ROM.
import java.io.*; class FilePrint { public static void main (String args[]) { System.out.println(Enter file name: ); char c; StringBuffer buf = new StringBuffer(); try { while ((c = (char)System.in.read()) != \n) buf.append(c); RandomAccessFile file = new RandomAccessFile(buf.toString(), rw); while (file.getFilePointer() < file.length()) System.out.println(file.readLine()); } catch (Exception e) { System.out.println(Error: + e.toString()); } } }
The FilePrint program begins very much like the FileInfo program in that it prompts the user to type in a filename and stores the result in a string. It then uses that string to create a RandomAccessFile object in read/write mode, which is specified by passing rw as the second parameter to the constructor. A while loop is then used to repeatedly call the readLine method until the entire file has been read. The call to readLine is performed within a call to println so that each line of the file is output to the screen.
Whew, this chapter covered a lot of ground! Hopefully youve managed to make it this far relatively unscathed. On the up side, youve learned about all there is to know about fundamental Java I/O and the most important classes in the I/O package. Thats not to say that there isnt still a wealth of information inside the I/O package that you havent seen. The point is that the Java class libraries are very extensive, which means that some of the classes will only be useful under very special circumstances. The goal of this chapter was to highlight the more mainstream classes and methods within the I/O package.
If you think youve had enough talk about I/O, then maybe youre ready for the next phase of Java: applet programming. Youve survived this far, so you might as well push on and start reaping the real benefits of knowing the Java language and class libraries inside and out. The next section of the book is focused on applet programming, which enables you to embed graphical Java applications into HTML pages. Have fun!