Reading and Writing Files

   

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 Access

At 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
Method Description
close() Closes the file
getFD () Gets a FileDescriptor object for the file
getFilePointer () Gets the location of the file pointer
length() Gets the length of the file
read() Reads data from the file
readBoolean () Reads a boolean value from the file
readByte () Reads a byte from the file
readChar () Reads a char from the file
readDouble () Reads a double floating-point value from the file
readFloat () Reads a float from the file
readFully () Reads data into an array, completely filling the array
readInt () Reads an int from the file
readLine () Reads a text line from the file
readLong () Reads a long int from the file
readShort () Reads a short int from the file
readUnsignedByte () Reads an unsigned byte from the file
readUnsignedShort () Reads an unsigned short int from the file
readUTF () Reads a UTF string from the file
seek () Positions the file pointer in the file
skipBytes () Skips over a given number of bytes in the file
write() Writes data to the file
writeBoolean () Writes a boolean to the file
writeByte () Writes a byte to the file
writeBytes () Writes a string as bytes
writeChar () Writes a char to the file
writeChars () Writes a string as char data
writeDouble () Writes a double floating-point value to the file
writeFloat () Writes a float to the file
writeInt () Writes an int to the file
writeLong () Writes a long int to the file
writeShort () Writes a short int to the file
writeUTF () Writes a UTF string

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.java
 import 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 Security

When 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.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net