CS 1054: Lab 8

Lab meeting 8: Using files in Java - II

The purpose of this lab is to expand your knowledge about file input/output in Java. Specifically, we will learn how to read some numbers from a file, perform manipulations on those numbers with the StringTokenizer class and write the output to another file.

Using files

  1. Start the BlueJ environment by clicking on Start->Programs->BlueJ.
  2. Open a new project in BlueJ. Name the project FileIO.
  3. Create a new class by clicking on "New Class" on the left. Name the class FileIOTest.
  4. Declare a field variable in your class of type String called inputFileName. Remember that its good programming practise to declare field variables as private variables.
  5. Initialize it to the string input1.txt in the constructor method of your class. Download file input1.txt to the directory where your project is stored. If you open this file, you can see that it comprises of 20 numbers, 2 on each line. You will read in the two numbers, multiply them and store the products in an output file.
  6. Declare another field variable of type String called outputFileName.
  7. Initialize it in the constructor to output1.txt.
  8. Also declare the following three field variables of the corresponding types shown below.
        private FileInputStream fis; 
        private BufferedReader br;
        private PrintStream ps;
     					
  9. Create new objects from these variables for reading and writing using the code below in the constructor method.
        fis = new FileInputStream(inputFileName);
        br = new BufferedReader(new InputStreamReader(fis));
        ps = new PrintStream(new FileOutputStream(outputFileName));
    					
    The code so far should be familiar to you from the previous lab last week.
  10. Declare a constant variable called SIZE and initialize it to 10. Remember that the keyword final identifies a variable to be a constant. It indicates that the value of that variable cannot be altered in the program.
  11. Declare two arrays of type int.
  12. Initialize their size to be of the variable SIZE in the constructor method.
  13. Create a new method of return type void called readAllNums(). In this method, use a for loop that will iterate SIZE number of times. Within the for loop, use the readLine() method of the BufferedReader class to read in a new line from the input file. Use a StringTokenizer object to break this line, which comprises of two numbers, into two Strings. Remember that one way of using StringTokenizer is the following:
    StringTokenizer stk = new StringTokenizer(str)
    where str is of type String. Use the nextToken() method of the stk object to extract the Strings.
  14. Now we want to convert these Strings into int types to be able to store them into the arrays that we created earlier. One way of doing this is to use the Integer class as follows:
    firstnum[i] = Integer.parseInt(firststr)
    where firstnum is the first int array that you created and str is of type String. This should all be done in the readAllNums() method. For more information about the Integer class, you can refer to the J2SE API.
  15. Create a second method of return type void called writeProductNums(). In this method, again use a for loop that iterates SIZE number of times. Store the product of the two numbers that you extract from each array into a local int variable called product. Within this loop, use the println() method associated with the PrintStream object ps to print the product to the output file.
  16. Compile your program and get rid of all compilation errors. Remember to "throw Exception" for each method in your class. Once you have eliminated all the errors, create an object. Invoke the readAllNums() method and then the writeProductNums() method. You will not see anything on the screen, but the file output1.txt should be created in your directory. The contents of the output file should be as follows:
    1 X 4 = 4
    2 X 5 = 10
    34 X 6 = 204
    41 X 7 = 287
    59 X 21 = 1239
    60 X 34 = 2040
    74 X 67 = 4958
    10 X 100 = 1000
    20 X 32 = 640
    75 X 75 = 5625
    				
  17. Close the ps and br objects that you created earlier at the end of each method by invoking their close() methods. You can get more information about these two classes by reading the J2SE API documentation (Shift+click to open in another window). Scroll to the BufferedReader and PrintStream classes in the bottom left frame and click on them to get the documentation about the classes (including their methods) in the right frame.
  18. Show your work to the GTA.

© Mir Farooq Ali 2003.