Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 11

Laboratory 11 - Text Files, Exceptions, and Strings

Goals:
To develop background for project 5: Mining data from a web log.
Getting started:
The Steps of the Lab:
  1. Open the iomodule project.
    Examine

    This project has one class, FileIO. When you open the class in the class editor, you will see the following fields that define the class:

        BufferedReader inputFile;  //An object to allow read (or input) access to a text file
        String inputFileName;      //The name of the input file
        BufferedWriter outputFile; //An object to allow write (or output) access to a text file
        String outputFileName;     //The name of the output file
        ArrayList dataList;        //An arrayList used to hold data from the input file
                       
    

    A typical program

    1. reads lines from an input file one at a time, putting part or all of the line in a list for some processing activity,
    2. processes the list of data, and
    3. writes the list one line at a time to an output file.

    This is referred to as Input-Process-Output (IPO).

    The iomodule project folder contains a data (text) file that was extracted from the department's web server log on Jupiter. You will be using this file later in this lab.

    • First we will look at exception handling: Textbook sections 12.1 - 12.5
    • Next we look at file input/output: Textbook sections 12.9.3 (the book uses a FileWriter object only) and 12.9.4
    • Finally we use String methods to process the data: Java help - the String class
    Try

    Exception handling. An exception is a special kind of event that is triggered by some kind of error as a program runs. For example, in the calculator project you prevented division by zero by looking at the divisor and displaying a message if it was zero. Had you actually divided by zero, an error message would have been displayed and the method that caused the error terminated. It is possible to "catch" the exception and handle the error in a special way of your choosing. To see how this is done create an instance of FileIO and

    1. invoke the checkDivZero() method as is, observing the result; then
    2. uncomment the try - catch statements and invoke the method a second time, again observing the results.

    Some exceptions must be handled, at least minimally, because the compiler requires it. These are called checked exceptions, and, if nothing else is done, a method that contains statements that might cause such an exception must at least have a throws clause added to the method signature:

        public boolean setInputFile() throws IOException
                       
    

    Our input/output methods must have this clause. Read the setInputFIle() method. The first part uses a dialog box so you can choose the file you want to use in a familiar way. The try - catch part actually tries to open the chosen file. Invoke the method once by choosing a valid file name, again by typing the name of a non-existant file in the dialog box text box, and once more by clicking the dialog box Cancel button. You should be able to predict what might happen in each case. A similar method is used to open a file for output (writing).

    Do

    File input/output. Read the processData() method and then invoke it. It will ask you for an input file and then display lines from the file one at a time. Use the file access_logcps.txt. It make take a minute or so to finish. The last line of the file, which comes from a www.csc.depauw.edu web log, is:

    65.54.188.106 - - [09/Nov/2004:13:25:30 -0500] "GET /%7Edegger/csc496/Assignment3/index.php HTTP/1.0" 200 1999
    

    Modify the method so it doesn't display lines from the file, but counts the number of lines and displays that number when finished.

    Modify the method again by adding code to add each line to the ArrayList dataList. When you are finished, inspect the dataList to see that it has the correct number of items and that the items look like they are from the web log.

    Now read the copyFile() method. It writes each line from the dataList to the new file that you select. Invoke the method. Verify that the orignal access_logcps.txt and its copy are the same. You can use notepad to inspect each one.

    Read

    Manipulating Strings. Use BlueJ help to go to the Java Class Libraries web site. Read about the compareTo, equals, indexOf(String str), and substring(int beginIndex, int endIndex) methods. What would each of the following do?

    "miller".compareTo("jones");
    "miller".equals("jones");
    "miller".substring(0, 4);
    "miller".indexOf("ll");
    "Hello Dolly".substring(0, "Hello Dolly".indexOf(" "));
                       
    
    Do

    Modify the processData method so that only the ip address at the beginning of each line of the input is added to the dataList. (Hint: use the last of the above examples.) Test your results.

    Question

    What does the mystery method do? After you answer this question, delete the line int outer = 0; and uncomment the other two lines to enable the outer loop. Invoke the method after running the processData method. What is the result? You will be discussing this in class to prepare for project 5.

Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University , Computer Science Department , Spring 2005
Maintained by Brian Howard ( bhoward@depauw.edu ). Last updated