4d

Advanced Placement

Computer Science Lab

 

 

File Input and Output

 

 

 

 

Introduction

 

In previous programs we have either .incorporated data into the program or read data from the keyboard.  In this exercise we will learn how to acquire data from a file.  Files can be of two types either text or binary.  Text files are just what the name implies.  A text file can be manipulate in a program such as Notepad or Wordpad.  Generally it is not a good idea to try a make text files with Word or some other full feature word processor because they incorporate a lot of internal codes into the file.  A binary file is a file of ones and zeros although sometimes when opened in a text editor you can see certain features of the file.  binary files are usually processed faster and so large data bases are often stored as binary files.  In this exercise we are going to consider text files only for the time being.  The code listed below is a class that allows you to open a read a text file one line at a time.  The name of the file is transferred to the class through the constructor.  If the file is in the same directory as the class file than all that is needed is the text file name in quotes.  If the file is some other location you also supply the path, e.g. “A:\text.txt”.  In Java an object from which we read or to which we write a sequence of bytes is called a stream.  In the code below the stream object is instantiated with  the line  FileReader fr = new FileReader(fileName);”. Note that “fileName” is the String with the file name of the file that we wish to read.  Next to actually read the file we instantiate an object that takes as its parameter the stream object.  The remainder of the code reads the file into a string, one line at a time.  Note that this is done withint a “try” and “catch” context.  This allows us to test whether or not the file exists and whether it can be read..  Also note that when we are done reading the file we closes the file.

 

 

 

 

import java.io.*;

 

public class TextReader

{

     private String fileName;

     private String outputText;

    

     //constructor

     TextReader(String s)

     {

          fileName = s;

          outputText = "";

    

     }//end TextReader

         

     //method to open/read text file into a string

     public void fileOpenRead()

     {

          String line = "";

         

          try

          {

              FileReader fr = new FileReader(fileName);

              BufferedReader inFile = new BufferedReader(fr);

             

              //read first line of file

              line = inFile.readLine();

                                                                            

              while (line != null)

              {

                   line = inFile.readLine();

                   outputText = outputText + line;

              }// end while

                            

              inFile.close();

          }//end try

         

          catch (FileNotFoundException e)

          {

              System.out.println("The file " + fileName + " was not found.");

          }//end catch

         

          catch (IOException e)

          {

              System.out.println(e);

          }//end catch

         

     }//end fileOpenRead

    

    

     public void displayText()

     {

          System.out.println("File text is:");

          System.out.println();

          System.out.println(outputText);

     }//displayText

 

}//end class

 

 

 

 

In much the same way we can also write a file to a text file:

 

 

 

 

 

import java.io.*;

 

public class TextWriter

{

     private String fileName = "test.txt";

     private String fileText = "";

    

     //default constructor

     TextWriter()

     {

          fileText = "This is text to be put in a file";

    

     }//end TextWriter

    

     //constructor with input text

     TextWriter(String s)

     {

          fileText = s;

    

     }//end TextWriter

    

 

     //method to write string into text file

     public void fileOpenWrite()

     {

          try

          {

         

              FileWriter fw = new FileWriter (fileName);

              BufferedWriter bf = new BufferedWriter(fw);

              PrintWriter of = new PrintWriter(bf);

              of.print(fileText);

              of.close();

             

          }

    

          catch (IOException e)

          {

              System.out.println(e);

          }//end catch

         

     }//end method

 

 

 

 

Note that in this class we have two constructors.  One allows us to create a default file and the other allows us to transfer a string to the instantiated class to then transfer to the file.

 

 

 

 

 

Procedure

 

A.  Reading a file

 

1.         Start a new application which is going to be a user of TextReader.

 

2.         Create a text file and place it in the class fold..

 

3.         Compile and execute the program and display the text file on the screen.

 

B.  Writing a file

 

1.         Start a new application which is going to be the user of TextWriter.

 

2.         Write some text to a file and then check the results with Notepad.

 

3.         Write a new constructor that will take both the file name and the input string.

 

 

 

 

 

Programming Assignment

 

Write a program to input and print out the following student test information.  Copy this text in notepad and then paste the file into the same directory as your class files.

 

Jones,Tom,2847,89,78,91,75,82

Smith,Mary,1143,67,78,88,77,88

Miller,Anne,4848,78,78,72,67,83

Brown,Robert,5630,74,83,88,80,86

Martinez,Ceasar,7892,74,78,81,75,72

Lopez,Maria,3461,87,77,91,75,92

 

Write the program to edit the student information and rewrite the file to disk, to add a student or delete a student.  Write the program to include the test average, student average, and standard deviation.  For interaction with the user use the JOptionPane dialog boxes.

 

Turn in a program listing your source code.  Source code should use proper formatting.  Also turn in a copy of your output as in previous assignments.  This can be obtained by running the program and then using AltPrintScreen to copy the window of the output onto the clipboard. Then paste the contents of the clipboard into WordPad and print out the document.  In addition to the source code, the output, also turn in the answer to the questions listed below.