I am using a JOptionPane to ask the user for a file, which I then give to BufferedReader. However, my code keeps throwing a FileNotFoundException. Could someone please help me to understand why.
Here is my code…
edit = JOptionPane.showInputDialog("Enter a file to edit");
try {
BufferedReader fIn = new BufferedReader(new FileReader(edit));
String in;
try {
while((in = fIn.readLine()) != null) {
System.out.println(in);
}
fIn.close();
}
catch (IOException ex) {
Logger.getLogger(WordProcessor.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fIn.close();
}
catch (IOException ex) {
Logger.getLogger(WordProcessor.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "File does not exist");
}
Here you should do ex.printStackTrace() it will reveal exact problem.
Note that current directory of program will be one from where it is launched so any relative path should be in relation to that not the source directory..