CSE 201 Homework 7


Read Section 4.12 in Chapter 4, and Section 12.1 in Chapter 12 in the textbook, and then answer the following questions.

  1. Write a statement that declares a BufferedReader object and initializes it to get input from the file named "input_file".
  2. 
           
  3. Implement (i.e., write the body of) the following method, countLines, that, given a BufferedReader parameter, counts and returns the number of lines of text in the corresponding file/stream.
  4.     public static int countLines(BufferedReader in) throws IOException
    
  5. Implement a new version of the countLines method that catches the possible IOException exception and outputs an error message if such an exception is raised.
  6.     public static int countLines(BufferedReader in)
    
  7. Implement the following method, sumLines, that, given a BufferedReader parameter, reads all the integer values appearing one per line in the corresponding file/stream and returns the sum of the integers. Make sure you catch and handle the possible IOException exceptions.
  8.     public static int sumLines(BufferedReader in)
    
  9. Write a statement that declares a PrintWriter object and initializes it to send output to the file named "output_file".
  10. 
           
  11. Implement the following void method, copyFile, that, given a BufferedReader parameter and a PrintWriter parameter, copies the file/stream from the given BufferedReader to the file/stream of the given PrintWriter. Make sure you catch and handle the possible IOException exceptions.
  12.     public static void copyFile(BufferedReader in, PrintWriter out)