Adding a method for reading files

This section guides you through creating a method called readToDoFile(). This method reads the contents of a list file line by line and adds each line to the DefaultListModel1 instance.

  1. Select the ToDoList class.
  2. Select Add and then Method from the Selected menu. When the Create Method SmartGuide appears, enter the following method name:
    void readToDoFile()
    
  3. Select Finish to generate the method.
  4. Select the new method and add the code to implement it. If you are viewing this document in a browser, you can select the following code, copy it, and paste it into the Source pane. The finished method should look like this:
    public void readToDoFile() {
       FileReader fileInStream;
       BufferedReader dataInStream;
       String result;
       try {
          // read the file and fill the list
          fileInStream = new FileReader(FILE_NAME);
          dataInStream = new BufferedReader(fileInStream);
     
          // clear the existing entries from the list
          getDefaultListModel1().removeAllElements();
     
          // for each line in the file create an item in the list
          while ((result = dataInStream.readLine()) != null) {
             if (result.length() != 0)
                getDefaultListModel1().addElement(result);
          }
          fileInStream.close();
          dataInStream.close();
       } catch (Throwable exc) {
            handleException(exc);
       }
       return;
    }
    
  5. Select Save from the Edit menu to save your changes and recompile.

Before continuing with the next task, let's review the code in this method:

  1. At the beginning of the method, there are declarations of the fields that are used to manipulate the file and its contents.
    FileReader fileInStream;
    BufferedReader dataInStream;
    String result;
     
    
  2. Next, statements associate the file with a FileReader and associate the FileReader with a BufferedReader. Using a BufferedReader makes it possible to read the file a line at a time. You defined the static FILE_NAME variable previously.
    try {
       // read the file and fill the list
       fileInStream = new FileReader(FILE_NAME);
       dataInStream = new BufferedReader(fileInStream);
     
    
  3. Next, we clear the list. Then, a loop reads the file one line at a time into the String result. If result is not a zero-length String, it adds the value of result to the list model.
    // clear the existing entries from the list
    getDefaultListModel1().removeAllElements();
     
    // for each line in the file create an item in the list
    while ((result = dataInStream.readLine()) != null) {
       if (result.length() != 0)
          getDefaultListModel1().addElement(result);
    }
     
    
  4. At the end of the try block, statements close the streams associated with the file.
    fileInStream.close();
    dataInStream.close();}
     
    
  5. The catch block passes any exceptions to handleException(). VisualAge for Java generates this method for all visually composed classes. The comment delimiters have been removed.
    catch (Throwable exc) {
       handleException(exc);
       }
    }
    ...
    handleException(Throwable exc) {
     
    /* Uncomment the following lines to print uncaught exceptions to stdout */
    	System.out.println("--------- UNCAUGHT EXCEPTION ---------");
    	exception.printStackTrace(System.out);
    }