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      All lectures
         FileInputStream Class    FileOutputStream Class    FileWriter Class    StringTokenizer Class

Simple File Handling

Download test1.fil and load it into the applet by typing test1.fil in the topmost textfield and clicking the Read button. Change the delimiter and repeat to see the possible interpretations.

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

class FileOps1 {
   File1 f1;
   FileOps1 (File1 f1) {
      this.f1 = f1;
   void readFromFile (String filename, String token) {
      try {
         FileInputStream fis = new FileInputStream(filename);
         BufferedReader is = new BufferedReader(new FileReader(fis.getFD()));
         StringTokenizer t = new StringTokenizer(is.readLine(), token);
         while (t.hasMoreTokens()) {
            f1.text.append (t.nextToken()+"\n");
      } catch (IOException e) {
         f1.messages.setText("File "+filename+" Not Found");

   public void writeToFile(String filename, String token) {
      try {
         FileOutputStream fos = new FileOutputStream(filename);
         FileWriter os = new FileWriter(fos.getFD());
         String toFile = f1.text.getText();
         StringTokenizer t = new StringTokenizer(toFile, "\n");
         while (t.hasMoreTokens()) {
      } catch (IOException e) {
         f1.messages.setText("Could not write to "+filename);

public class File1 extends Applet implements ActionListener {
   JTextArea text;
   JTextField filename, messages, delimiter;
   JButton read, write;
   FileOps1 fo;
   public void init () {
      setLayout(new BorderLayout());
      Panel p = new Panel();
      p.setLayout(new GridLayout(2,1));
      Panel q = new Panel();
      q.setLayout(new BorderLayout());
      Panel r = new Panel();
      r.setLayout(new GridLayout(2,1));
      r.add(filename = new JTextField());
      r.add(new JLabel("Filename", JLabel.CENTER));
      q.add("Center", r);
      r = new Panel();
      r.setLayout(new GridLayout(2,1));
      r.add(delimiter = new JTextField("|"));
      r.add(new JLabel("Delim", JLabel.CENTER));
      q = new Panel();
      q.setLayout(new GridLayout(2,1));
      q.add(messages = new JTextField());
      q.add(new JLabel("Messages", JLabel.CENTER));
      add ("North", p);
      add ("Center", new JScrollPane(text = new JTextArea(30,30)));
      p = new Panel();
      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 FileOps1(this);
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == read)
         fo.readFromFile(filename.getText(), delimiter.getText());
      if (evt.getSource() == write)
         fo.writeToFile(filename.getText(), delimiter.getText());