0

I'm getting a file not found exception from this code even though it's within the try catch statement and I'm not sure what's wrong, the file is within the project folder and is called 'someFile.txt'. This is the main method:

public static void main(String[] args) {
    if (args.length == 0) {
        System.out.println("no arguments given");
        return;
    }
    double FRE = sortFile(args[0]);
    System.out.println("Readability of file " + args[0] + "= " + FRE);

}

And this is the sortFile method where the exception occurs:

public static double sortFile(String FileName) {
    int nWords = 0;
    int nSyllables = 0;
    int nSentences = 0;
    File text = new File(FileName);
    try {
        Scanner sc = new Scanner(text);
        while (sc.hasNext()) {
            contents.add(sc.next());
            ++nWords;
        }
        sc.close();
        for (String e : contents) {
            getNumSyllables(e);
        }

    } catch (FileNotFoundException e) {
        System.out.println("The file" + FileName + "could not be opened.");
        e.printStackTrace();
    }
    double FRE = getFRE(nWords, nSyllables, nSentences);
    return FRE;
}

Thanks for any help :)

8
  • Could you please show the project hierarchy, it would help more than this code. Commented May 9, 2014 at 9:35
  • 1
    Just an advice. Always close your streams etc in the finally block after the catch statement. That way it will 100% sure be closed. You're also trying to create the file above the try. Place File text = new File(FileName); in the try. Commented May 9, 2014 at 9:38
  • seriously you are getting a filenotfoundexception even though its within the try and catch block, but there is no code after that which prints something and you print the stackstrace, so you cant know if the exception was caught or not... Commented May 9, 2014 at 9:40
  • @kai He could debug / see his logs to see if the exception is catched. The problem though is probably that the file couldn't be found in the location you used. Commented May 9, 2014 at 9:41
  • 1
    If you use Java 7 use java.nio.file; at least the exception you'll have will be meaningful Commented May 9, 2014 at 9:54

3 Answers 3

3

well, the file does not exist in that location. Try to add

System.out.println(text.getAbsolutePath())

to see where the file is expected. Note, when you provide a relative path (e.g. some/path/filename.ext), this is relative to the working directory. The working directory is the folder your java program is started in.

If you're using an IDE (e.g. Eclipse, IntelliJ, Netbeans) you can define the working directory in your run configuration.

See:

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

Comments

1

I'm getting a file not found exception from this code even though it's within the try catch statement

The try-catch does not prevent the Exception from being thrown. It merely executes the code in the catch block when an Exception is thrown, and you are just printing the stack trace in the catch block, which is what usually printed anyways on uncaught exceptions.

To resolve your actual issue, first try passing the full path to the file, verify that it works and then use Tim's answer to debug your absolute path.

Comments

0

Try launching your program with the absolute path.

java yourclassname absolutepath_to_someFile.txt

Comments

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.