![]() |
![]() |
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:
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:
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);
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);}
![]() |
![]() |