loader image
Skip to main content
If you continue browsing this website, you agree to our policies:
x
Completion requirements

This chapter discusses Java's FileReader and BufferedReader classes in detail. FileReader and BufferedReader classes are used together when reading data from an external file. The use of the BufferedReader class allows data to be buffered as it is read from a file before manipulating it. The readLine() method of the BufferedReader class reads a line of text from a character-oriented input stream, and puts it into a new String object.

10. Opening Files


Answer:

Yes.

Opening Files

Look at the openFiles() method. It opens a BufferedReader stream with the source file and a PrintWriter with the destination file. Decide what should go in each box, and click it to verify your choice.

class CopyMaker
{
  String sourceName, destName;
  BufferedReader source;
  PrintWriter dest;
  String line;

   // return true if both files open, otherwise return false
   //
   private boolean openFiles()  
   {
     // open the source
     try
     {      
       source = new (new FileReader( sourceName ));
     }
     catch (  iox )
     {
       System.out.println("Problem opening " + sourceName );
       return false;
     }

     // open the destination
     try
     {      
       dest = new PrintWriter( 
           new (new FileWriter( destName )) );
     }
     catch (  iox )
     {
       System.out.println("Problem opening " + destName );
       return false;
     }

     return   ;
   }

    . . . . .  the rest of the program . . . . .
   
}


Question 10:

Click in the blanks.