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    PrintWriter Class    StringTokenizer Class

PrintWriter

This one uses PrintWriter to achive the same results as before. Here is a Sample file.

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

class FileOps2 {
   File2 f2;
   
   FileOps2 (File2 f2) {
      this.f2 = f2;
   }
   
   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()) {
            f2.text.append (t.nextToken()+"\n");
         }
         is.close();
      } catch (IOException e) {
         f2.messages.setText("File "+filename+" Not Found");
      }
   }

   // Use PrintWriter to send various binary data items
   // instead of FileWriter
   public void writeToFile(String filename, String token) {
      try {
         FileOutputStream fos = new FileOutputStream(filename);
         PrintWriter os = new PrintWriter(fos);
         String toFile = f2.text.getText();
         StringTokenizer t = new StringTokenizer(toFile, "\n");
         while (t.hasMoreTokens()) {
            StringTokenizer r = new StringTokenizer(t.nextToken(), " ");
            while (r.hasMoreTokens()) {
               os.print(r.nextToken());
               os.write(token);
            }
            os.print(token);
         }
         os.close();
      } catch (IOException e) {
         f2.messages.setText("Could not write to "+filename);
      }
   }
}

public class File2 extends Applet implements ActionListener {
   JTextArea text;
   JTextField filename, messages, delimiter;
   JButton read, write;
   FileOps2 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 GridLayout(2,1));
      q.add(messages = new JTextField());
      q.add(new JLabel("Messages", JLabel.CENTER));
      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 FileOps2(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());
   }
}