Creating a new method: adding a method for reading files

This section guides you through creating a method called readToDoFile that reads an input file.

Before creating this method, let's review what it is supposed to do:

Here are the detailed steps for creating this method:

  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(File dirName, File fileName, DefaultListModel fillList)
  3. This specifies a method that takes three arguments.
  4. Select Finish to generate the method.
  5. Select the new readToDoFile 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  (File dirName, File fileName,
            DefaultListModel fillList) {
            FileReader fileInStream = null;
            BufferedReader dataInStream;
            String result;
            // if valid directory and filenames have been passed in, 
            // read the file and fill the list
            if ((dirName != null) && (fileName != null)) {
               try {
                       fileInStream= new FileReader(fileName);
               }
               catch (IOException e) { 
                       System.err.println
                       ("IO exception opening To-Do File " +fileName);
                       return;       
               }
               dataInStream = new BufferedReader(fileInStream);
               // clear the existing entries from the list
               fillList.removeAllElements();
               try {
                       // for each line in the file create an item in the list
                       while ((result = dataInStream.readLine()) != null)  {
                               if (result.length() != 0)
                                       fillList.addElement(result);                                                 
                       }
               }
               catch (IOException e) {System.err.println
                     ("IO exception reading To-Do File " +fileName);}
               try {
                       fileInStream.close();
                       dataInStream.close();
               }
               catch (IOException e) { System.err.println
                     ("IO exception closing To-Do File " +fileName);}       
            }
            else {
                    System.err.println
                       ("Null file name and/or directory reading To-Do File");      
            }       
        return;
    }

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, and an if statement that tests whether both  the directory and the file name are non-null:
    FileReader fileInStream = null;
    BufferedReader dataInStream;
    String result;
    // if valid directory and filenames have been passed in, 
    // read the file and fill the list
    if ((dirName != null) && (fileName != null)) {
  2. Next, there are statements to associate the file with a FileReader and to associate the FileReader with a BufferedReader. Using a BufferedReader makes it possible to read the file a line at a time.
    try {
            fileInStream= new FileReader(+fileName);
    }
    catch (IOException e) { 
            System.err.println("IO exception opening To-Do File "
                    +fileName);
            return;  
    }
    dataInStream = new BufferedReader(fileInStream);
  3. Next, we clear the fillList. Then, a loop reads the file one line at a time into the String result. Then, if result is not a zero-length String, it adds result as an item to fillList:
    fillList.removeAllElements();
    try {
            // for each line in the file create an item in the list
            while (((result = dataInStream.readLine()) != null) ) {
                    if (result.length() != 0)
                            fillList.addElement(result);                                                    
            }
    }
    catch (IOException e) {System.err.println("IO exception reading To-Do File "
            +fileName);}
  4. Finally, there are statements to close the streams associated with the file:
    try {
            fileInStream.close();
            dataInStream.close();
    }
    catch (IOException e) { System.err.println("IO exception closing To-Do File "
            +fileName);}