Fairly new to Java and I'm trying to use a fileChooser
to open a file and read the information. I'm at the stage where I am trying to create a fileInputStream and inputStreamReader
. When creating I was being given a FileNotFoundException
despite the file existing. Not too clear why this occurs but I've placed this code into a try/catch
block to resolve it. Unfortunately, I still get a cannot find symbol
error during compilation for the variable "in". If anyone could give an explanation to these issues it would be greatly appreciated.
openFileBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setDialogTitle("Open your Rain Data.");
int returnVal = fileChooser.showOpenDialog(null);
//Handles when a file is opened by the user.
if (returnVal == JFileChooser.APPROVE_OPTION) {
String absolutePath = fileChooser.getSelectedFile().getAbsolutePath();
File file = new File(absolutePath);
try {
FileInputStream in = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(in);
} catch (FileNotFoundException ex) {
System.out.println("Error - the file was unable to be read by the rain handler. Check permissions and the file.");
} finally {
System.out.println(file.canRead());
System.out.println(file.exists());
System.out.println(in.available());
}
}
}
});