CSCI 180/181 Lab 9

Due: 11:59 pm on March 31, 2008 – handin name: lab9

Objectives

Java is pretty keen but is not reknowned for the easy-use of its input/output (i/o). This is perhaps because it is designed for serious use, not just those of us learning to program.

  1. Create a bluej project called Lab9
  2. Create a class called Lab9 whose constructor creates a BufferedReader for reading input from the terminal. (See TokenExamp.java if you need a model.)
  3. Add a method to the Lab9 class called echo that simply inputs a string from the terminal and outputs it again.
  4. Add another BufferedReader instance variable to your class, called fileInput and add a method called void openFileForReading(String filename). This method should open a file and associate it with fileInput. To open a file, the syntax is exactly the same as opening input from the terminal, except you replace new InputStreamReader(System.in) with new FileReader(filename)

Well, not quite. You also need to put a try/catch block around this, because the file might not exist, or there could be some other error. So, make the body like this

 
try
{
   fileInput = new BufferedReader(new FileReader(filename));
}
catch(FileNotFoundException e)
{
   System.out.println("Unable to find " + filename);
}
catch(IOException e)
{
  System.out.println("IO Error: " + e); // in case something else
}

Catching the exceptions (input/output errors) also requires you to import the exceptions from java.io. You may want to simply place the line import java.io.*; in your import section.

Test this method by calling it with README.TXT as the file. Then test it by calling the method with a file that does not exist.

  1. BufferedReaders provide a readLine method to read lines from a file (or the terminal). It returns null when it has read to the end of the file.

Add a method void echoFile() that echoes out the entire file associated with the fileInput variable. Your method should loop, reading a line, outputting it, stopping when the line is null. (Don't forget that this also needs to be in a try/catch block in case an i/o error occurs.)

  1. Just as we can read files, we can write them. In this case we only use the FileWriter class. Create a FileWriter instance variable called fileOutput and add a method openFileForOutput(String filename). This will be similar to the open for reading, except we only catch IOExceptions and we only use the FileWriter class (no BufferedReader or Writer class).
  2. Write a void copyFile(String originalFilename, String copyFilename) method that copies a file into a new file by inputting it and echoing it to the new file. The syntax of writing is slightly different from the System.out.println you're used to. In our example,
 
try
{
   fileOutput.write("foo\n");
}
catch (IOException e)
{
   System.out.println("IO Error: + e);
}

would write foo to the output file and then an end-of-line, similarly to using

 
System.out.println("foo");

to write foo to an the standard output terminal. (Notice that when writing anything to a file, we again need to be in a try/catch block that catches an i/o error.

 

  1. A useful exercise (in terms of project 3)  is to read in a line, tokenize it, and decide whether it is a room creation line (starts with “room”), an exit creation line (starts with “exit”), or an item creation line (starts with “item”). For this lab, create a method:

public void processInputLine()

that reads in a line and prints one of the following where the number depends on the number of other words in the line.:

room 5 /* if room creation line with 5 other words */

exit 4 /* if exit creation line with 4 other words */

item 3 /* if item creation line with 3 other words */

 

Note that you can do this without using a string tokenizer, but you need to be familiar with the tokenizer for project3 so should use it for this method..

End notes

In project 3, you will need to be able to read in a file. You now know enough to do that, except for the question of where you need to put the file that you want to read. The surest bet is to place it inside the folder that holds the project, so your filename can simply be the name of the file. If it is anywhere else, you need to give a path to the file, in which case you should first create an object of type File, and then open the file, e.g. inputFile = new BufferedReader(new FileReader(new File(filename)));