1

I am a Java beginner and have read similar questions but still I dont get why my code is showing a FileNotFound Exception. My file is in the same directory.

My code is:

import java.io.*;
import java.util.Scanner;

public class reader {
    public static void main(String[] args) { 
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        double y = in.nextDouble();
        float g = in.nextFloat();
        String a = in.next();
        File file = new File("v.txt");
        System.out.println(x + "" + y + "" + g + "" + a); 
        Scanner inFile = new Scanner(new FileReader(file));
        String u = inFile.nextLine();
        System.out.println(file.getAbsolutePath());
        System.out.println(u);
    }
}

Error is:

17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
     Scanner inFile = new Scanner(new FileReader(file));
                                  ^
1 error
15
  • 1
    Same directory from where you are running the code? Commented Jan 3, 2015 at 15:30
  • Are you running your code from an IDE, and if so, what IDE? Commented Jan 3, 2015 at 15:32
  • @AniketThakur Yes. Both my .java file and .txt file are in the same folder. Commented Jan 3, 2015 at 15:33
  • Also you might want tp use exists() on file object before you perform read/write operations on it. Commented Jan 3, 2015 at 15:33
  • 2
    @edbale You've searched for something different. A FileNotFoundException will be thrown if the file couldn't be found during runtime. Your Error is triggered by the compiler, because your code is wrong (unhandled exception). This is something different and that is why I've asked you if you've searched for the error message. Commented Jan 3, 2015 at 15:44

1 Answer 1

5

You are encountering a compile time error:

error: unreported exception FileNotFoundException; must be caught or declared to be thrown
 Scanner inFile = new Scanner(new FileReader(file));

This is a simple way of fixing it:

public class reader {
   public static void main(String[] args) throws Exception { 
         //...
   }
}

although using try {...} catch(...){ } is a better way of dealing with the possible run time exception.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes it worked, but If I know that my file exists why do I need to put this ? Is it required whenever I access a file ?
"try-with-resources" would also be possible.
@edbale You know, but the compiler doesn't know that you know - and he wouldn't trust you or anybody else reading or writing this sentence ;-)
@edbale "but If I know that my file exists why do I need to put this?" are you sure that your file will always be there? Is it physically impossible to delete it?
Btw you should use scanner to "parse" files and FileReader + BufferedReader to read files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.