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 :)

user3405010
  • 21
  • 1
  • 4
  • Could you please show the project hierarchy, it would help more than this code. – Parth Soni May 09 '14 at 09: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. – GregD May 09 '14 at 09: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... – kai May 09 '14 at 09: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. – GregD May 09 '14 at 09:41
  • i know... i only want to tell him that the exception is seriously be catched. because he thought its not. – kai May 09 '14 at 09:42
  • 1
    If you use Java 7 use java.nio.file; at least the exception you'll have will be meaningful – fge May 09 '14 at 09:54
  • @fge using NIO2 is good advise, however java.io.File is not complete gone and he should learn to use that as well. Maybe we can fix the problem on hand before learing additional stuff? – Tim Büthe May 09 '14 at 09:58
  • @user3405010 are you using an IDE to code? Which one? – Tim Büthe May 09 '14 at 09:58

3 Answers3

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:

Community
  • 1
  • 1
Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
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.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

Try launching your program with the absolute path.

java yourclassname absolutepath_to_someFile.txt