Possible Duplicate:
Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
Thanks guy for solving my first issue, i am now getting a new error
import java.io.*;
import javax.swing.*;
public class FileBrowser {
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
String filename = file.getName();
System.out.println("You have selected: " + filename);
FileReader fr = new FileReader("filename");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
}
Error :
java.io.FileNotFoundException: filename (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at java.io.FileReader.<init>(FileReader.java:58)
at FileBrowser.main(FileBrowser.java:13)
is the error where it is not properly getting the file name from the file browser ?
In addition to Adel Boutrons and Mark Byers answers. You also need to make some changes.
Firstly You are choosing file chooser so you will also need
This will give absolute path of your file including your file name.
Here you are not giving any file name, just string, remove “filename”.
If file not found then it will throw FileNotFoundException.