-1

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 ?

Community
  • 1
  • 1
user1892955
  • 77
  • 1
  • 4
  • 9
  • If you have a new issue it's better to create a new question otherwise all the old questions and answers get mixed up with the new questions and answers and none of the comments make sense any more. You should probably revert the question to its original state and make a new one with your new question. – Mark Byers Dec 22 '12 at 00:27
  • Ok thanks a lot, is there an easy way to revert the question ? – user1892955 Dec 22 '12 at 00:39

3 Answers3

9

There are checked and unchecked exceptions in Java. Checked exceptions that your method throws must be declared. The FileReader constructor can throw a FileNotFoundException which is a checked exception. Some of the other method calls in your code also can throw checked exceptions.

If you call a method that can throw a checked exception then you either need to catch the exception and handle it or declare that your method throws this exception. You have to do one of these even if you believe that the exception will never be thrown. Failure to do so is a compile error.

Either add a throws:

public static void main(String[] args) throws IOException {

or surround the code with a try/catch block:

try {
    // ...
} catch (IOException e) {
    e.printStackTrace();
    // Or ask the user for a different filename...
}
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

surround FileReader fr = new FileReader("filename"); with try and catch as follow:

try {
     FileReader fr = new FileReader("filename"); 
} catch (Exception e) {
    System.out.println("Error: " + e);
}
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
0

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

   String path = file.getAbsolutePath();

This will give absolute path of your file including your file name.

   FileReader fr = new FileReader("filename"); 

Here you are not giving any file name, just string, remove "filename".

    FileReader fr = new FileReader(path.replace("\\", File.separator)); 

If file not found then it will throw FileNotFoundException.

Smit
  • 4,685
  • 1
  • 24
  • 28
  • Thank you very much :) it is working but i still get an error message even though it works, should i be worried ? – user1892955 Dec 22 '12 at 00:29
  • figured it out thanks guys for your help ! – user1892955 Dec 22 '12 at 00:30
  • @user1892955 Let me update the answer. – Smit Dec 22 '12 at 00:32
  • @user1892955 Just one suggestion. Study hard and try to find answers by your own then you will learn. – Smit Dec 22 '12 at 00:40
  • Thank you, do you have any good tutorial or handy sites for some one just starting out ? – user1892955 Dec 22 '12 at 00:44
  • @user1892955 I already gave in your previous post or you can just google for beginners tutorials. You will find tons of crawling in there. – Smit Dec 22 '12 at 00:45
  • Thanks :) once i have the contents of the file displayed, how can i use this information ?, for example i already have a piece of code that prints out text, but this text must be typed into the code, is there a way to use the text i have now retrieved using the code above for this purpose – user1892955 Dec 22 '12 at 00:55
  • it is saved a string s, so i could just use s as this already has my text and use ti to print from – user1892955 Dec 22 '12 at 00:57
  • @user1892955 I did not understand this question. Not even in your previous question. What exactly you have to do? print where? What you want to do with that file? Moreover `s` only store the String till `while` loop runs. – Smit Dec 22 '12 at 00:59