20-CS-694 Advanced Programming Techniques Spring 2012

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
File Operations and Classes

This series shows how to use many of the classes associated with file handling. Click on the class names in the following table to see class details.

FileInputStream FileOutputStream FileWriter PrintWriter JFileChooser
ObjectOutputStream ObjectInputStream RandomAccessFile Serializable StringTokenizer

1.   File1.java

  -   Open and read a text file - token delimiters can be set by the user. Edit the file and write it. Classes FileOutputStream and FileWriter are used to write the file.

Download test1.fil and load it into the applet by clicking the 'Choose File' button to bring up a file-chooser, select the file from the chooser, and clicking the 'Read' button. Change the delimiter in the top right textfield and repeat to see the possible interpretations. Click 'Write' to write the file.

2.   File2.java

  -   Same as above except the PrintWriter class is used to write. The text file is test2.fil which contains a number of | and ? to try as delimiters.
3.   File3.java

  -   Open, read, and write a binary random access file such as test3.fil. This illustrates the many methods of the RandomAccessFile class that support writing data in various ways. The test file test3.fil contains a sequence of ints.
4.   File4.java

  -   Read and write class objects. In this example the test file test4.fil has two objects: the first is a hash table, the second is a vector with the hash table as element 3. The code finds the second object, invokes the elementAt method to get the hash table. Then invokes the get method on the hash table to get the hash table elements.