If your file-reading needs are relatively simple, you can use the FileInputStream class, which is a simple input-stream class derived from InputStream. This class features all the methods inherited from the InputStream class. To create an object of the FileInputStream class, you call one of its constructors, of which there are three, as shown: FileInputStream(String name) FileInputStream(File file) FileInputStream(FileDescriptor fdObj) The first constructor takes a String as the argument and uses this String to create a FileInputStream object. The second constructor creates the object from a File object, and the third creates the object from a FileDescriptor object. As you might have guessed, the counterpart to the FileInputStream class is FileOutputStream, which provides basic file-writing capabilities. Besides FileOutputStream 's methods, which are inherited from OutputStream, the class features three constructors, whose signatures look like this: FileOutputStream(String name) FileOutputStream(File file) FileOutputStream(FileDescriptor fdObj) The first constructor here also takes a String as the argument and creates a FileOutputStream object with the given filename, whereas the second constructor creates the object from a File object. The third constructor creates the object from a FileDescriptor object. Random File AccessAt this point, you might think that Java's file-handling capabilities are scattered throughout a lot of different classes, making it difficult to obtain the basic functionality you need to read, write, and otherwise manage a file. But Java's creators are way ahead of you. They created the RandomAccessFile class for those times when you really need to get serious about your file handling. By using this class, you can do just about everything you need to do with a file. You create a RandomAccessFile object by calling one of the class's two constructors, whose signatures are as follows : RandomAccessFile(String name, String mode) RandomAccessFile(File file, String mode) The first constructor creates a RandomAccessFile object from a string containing the filename and another string containing the access mode ( r for read and rw for read and write). The second constructor creates the object from a File object and the mode string. After you have the RandomAccessFile object created, you can call on the object's methods to manipulate the file. Table 21.5 lists those methods. Table 21.5. Methods of the RandomAccessFile Class
Listing 21.6 is a Java application that takes a filename on the command line and then reads and displays the text file using a RandomAccessFile object. Listing 21.6 Source Code for RandomAccessExample.javaimport java.io.*; class RandomAccessExample { public static void main( String[] args ) { if ( args.length != 1 ) { System.out.println( "Usage: java RandomAccessExample <textfile>" ); System.exit( 0 ); } String fileName = args[0]; try { // Create a random access file for read only RandomAccessFile file = new RandomAccessFile( fileName, "r"); long filePointer = 0; long length = file.length(); // Keep going through the file until you reach the length of the file while ( filePointer < length ) { String s = file.readLine(); System.out.println(s); filePointer = file.getFilePointer(); } } catch ( IOException ex ) { ex.printStackTrace(); } } } File SecurityWhen you start reading and writing to a disk from a networked application, you have to consider security issues. Because the Java language is used especially for creating Internet-based applications, security is even more important. No user wants to worry that the Web pages he's currently viewing are capable of reading from and writing to his hard disk. For this reason, the Java system was designed to allow the user to set system security from within his Java-compatible browser and determine which files and directories are to remain accessible to the browser and which are to be locked up tight. In most cases, the user disallows all file access on his local system, thus completely protecting his system from unwarranted intrusion. In fact, the default setting on all current browsers disallows access to the local system, and until recently, even volunteering access to the file system was not possible. This tight security is vital to the existence of applets because of the way they are automatically downloaded onto a user's system behind the user's back, as it were. No one would use Java-compatible browsers if he feared that such use would open his system to any sort of tampering. Java standalone applications, however, are a whole different story. Java applications are no different from any other application on your system. They cannot be automatically downloaded and run the way applets are. For this reason, standalone applications can have full access to the file system on which they are run. The file-handling examples in this chapter, then, are incorporated into Java standalone applications. |