Я пытался прочитать файл открыт с помощью JFileChooser, и я получил следующие ошибкинезарегистрированный FileNotFoundException исключение при чтении файла
error: unreported exception FileNotFoundException; must be caught or declared to be thrown
BufferedReader br = new BufferedReader(new FileReader(file));
Вот фрагмент кода:
class LoadFile implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
File f = new File("/home/ralf/Documents");
String line = null;
chooser.setCurrentDirectory(f);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file));
try {
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(null, fnfe.getMessage());
return;
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
}
}
}
Примечание: Класс LoadFile является найденный в классе Animation (не показан), который расширяет JFrame. Я разместил кнопки в классе Animation consructor и оттуда вызвал действие LoadFilePerformed.
Я думаю, что сам JFileChooser находит путь к файлу ... или вы выбираете или просматриваете файл. вам не нужно явно указывать параметр File в его currentDirectory !!! – agpt