20-CS-694 Advanced Programming Techniques Spring 2012
File Classes

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

     Previous      Next     Sample file      Applet Source      RandomAccessFile Class     All lectures

Random Access Files

Sample file - this one is a binary file unlike the previous ones.

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

class FileOps3 {
   File3 f3;
   
   FileOps3 (File3 f3) {
      this.f3 = f3;
   }
   
   void readFromFile (String filename, String token) {
      try {
         RandomAccessFile fis = new RandomAccessFile (filename,"r");
         long numb = fis.length()/4;
         f3.text.setText("");
         for (long i=1 ; i <= numb ; i++) {
            f3.text.append (fis.readInt()+"\t");
            if ((i % 4) == 0) f3.text.append ("\n");
         }
         f3.text.append("\n---------------------\n");
         for (long i=numb-1 ; i >= 0 ; i--) {
            fis.seek(i*4);
            f3.text.append(fis.readInt()+"\t");
            if ((i % 4) == 0) f3.text.append ("\n");            
         }
         fis.close();
      } catch (IOException e) {
         f3.messages.setText("File "+filename+" Not Found");
      }
   }

   public void writeToFile(String filename, String token) {
      try {
         RandomAccessFile fos = new RandomAccessFile (filename,"rw");
         StringTokenizer t = new StringTokenizer(f3.text.getText()," ");
         while (t.hasMoreTokens()) {
            fos.writeInt(Integer.parseInt(t.nextToken()));
         }
         fos.close();
      } catch (IOException e) {
         f3.messages.setText("Could not write to "+filename);
      }
   }
}

public class File3 extends Applet implements ActionListener {
   JTextArea text;
   JTextField filename, messages, delimiter, number;
   JButton read, write;
   FileOps3 fo;
   
   public void init () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(2,1));
      JPanel q = new JPanel();
      q.setLayout(new BorderLayout());
      JPanel r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(filename = new JTextField());
      r.add(new JLabel("Filename", JLabel.CENTER));
      q.add("Center", r);
      r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(delimiter = new JTextField("|"));
      r.add(new JLabel("Delim", JLabel.CENTER));
      q.add("East",r);
      p.add(q);
      q = new JPanel();
      q.setLayout(new BorderLayout());
      r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(messages = new JTextField());
      r.add(new JLabel("Messages", JLabel.CENTER));
      q.add("Center", r);
      r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(number = new JTextField());
      r.add(new JLabel("Number", JLabel.CENTER));
      q.add("East", r);
      p.add(q);
      add ("North", p);
      add ("Center", new JScrollPane(text = new JTextArea(30,30)));
      
      p = new JPanel();
      p.setLayout(new GridLayout(1,3));
      p.add(read = new JButton("Read"));
      p.add(new JLabel());
      p.add(write = new JButton("Write"));
      add ("South", p);
   }
   
   public void start () {
      fo = new FileOps3(this);
      read.addActionListener(this);
      write.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == read)
         fo.readFromFile(filename.getText(), delimiter.getText());
      else if (evt.getSource() == write)
         fo.writeToFile(filename.getText(), delimiter.getText());
   }
}