Overview | Schedule | Announcements | Resources | Assignments | Home |
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
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.
|
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
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
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 |
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 |
Overview | Schedule | Announcements | Resources | Assignments | Home |
DePauw
University , Computer Science
Department , Spring 2005
Maintained by Brian
Howard ( bhoward@depauw.edu
).
Last updated