Cannot Find Symbol Class Ioexception [Duplicate]
Public Void populateNotesFromFile() { Try{ Bufferedreader Reader = New Bufferedreader(New Filereader(Default_Notes_Saved)); String fileNotes = Reader...
public void populateNotesFromFile()
{
try{
BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
String fileNotes = reader.readLine();
while(fileNotes != null){
notes.add(fileNotes);
fileNotes = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.err.println("The desired file " + DEFAULT_NOTES_SAVED + " has problems being read from");
}
catch (FileNotFoundException e){
System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
}
//make sure we have one note
if (notes.size() == 0){
notes.add("There are no notes stored in your note book");
}
}
Whenever i compile the above i get a message saying cannot find symbol class IOException e
can someone tell me how to fix it please :d
thanks
IOException is a class from the java.io package, so in order to use it, you should add an import declaration to your code. import java.io.*; (at the very top of the java file, between the package name and your class declaration)
FileNotFoundException is a IOException. It's a specialisation of IOException. Once you have caught the IOException, the flow of the program will never get to the point of checking for a more specific IOException. Just swap these two, to test for the more specific case first (FileNotFound) and then handle (catch) any other possible IOExceptions.
You need
import java.io;
at the top of your file.
Also, FileNotFoundException needs to be above IOException since it's a subclass of IOException.
Your probably missing an import reference to IOException class. I'ts located in the java.io package.
Can I suggest a litle change in you method? Always close the stream in a finally block:
public void populateNotesFromFile() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
String fileNotes = reader.readLine();
while (fileNotes != null) {
notes.add(fileNotes);
fileNotes = reader.readLine();
}
} catch (FileNotFoundException e) {
System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
} catch (IOException e) {
System.err.println("The desired file " + DEFAULT_NOTES_SAVED
+ " has problems being read from");
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// make sure we have one note
if (notes.size() == 0) {
notes.add("There are no notes stored in your note book");
}
}
You need to either import the java.io.* package at the top of the file, or fully-qualify the exception as java.io.IOException
Switch the order of FileNotFoundException and IOException