0

Currently trying to write a program to take input from a file and store it in an array. However, whenever I try to run the program the file cannot be found (despite file.exists() and file.canRead() returning true).

Here is my code:

    public void getData (String fileName) throws FileNotFoundException
{
    File file = new File (fileName);
    System.out.println(file.exists());
    System.out.println(file.canRead());
    System.out.println(file.getPath());
    Scanner fileScanner = new Scanner (new FileReader (file));
    int entryCount = 0;     // Store number of entries in file

    // Count number of entries in file
    while (fileScanner.nextLine() != null)
    {
        entryCount++;
    }

    dirArray = new Entry[entryCount];   //Create array large enough for entries
    System.out.println(entryCount);

}

public static void main(String[] args)
{
    ArrayDirectory testDirectory = new ArrayDirectory();


    try
    {
        testDirectory.getData("c://example.txt");
    }

    catch (Exception ex)
    {
        ex.printStackTrace();
    }

}

(In it's current state the method is only designed to count the number of lines and create the array)

The console output is as follows: true true c:/example.txt

The program seems to throw a 'FileNotFoundException' on the line where the scanner is instantiated.

One thing I have noticed when checking the 'file' object when debugging is although it's 'path' variable has the value "c:\example.txt", it's 'filePath' value is null. Not sure if this is relevant to the issue or not

EDIT: After Brendan Long's answer I have updated the 'catch' block. The stack trace reads as follows:

java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at assignment2.ArrayDirectory.getData(ArrayDirectory.java:138)
    at assignment2.ArrayDirectory.main(ArrayDirectory.java:193)

Seemingly the scanner doesn't recognize the file and thus can't find the line

AJCol
  • 91
  • 1
  • 8
  • 1
    possible duplicate of [Java new File() says FileNotFoundException but file exists](http://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists) – Brendan Long Apr 25 '14 at 16:41
  • @BrendanLong I visited that page before posting this and no solution worked, hence this question – AJCol Apr 25 '14 at 16:47
  • Why do you have two slashes in the path name? – ajb Apr 25 '14 at 16:49
  • @ajb That's just how I've been taught to do it. Either way it still recognises the address as file.getPath() returns "c:\example.txt" – AJCol Apr 25 '14 at 17:02
  • @AJCol I think somebody was confused. In Java, you have to double a backslash (\\) to use it in a string literal, but you don't need to do that with a regular slash (`/`). The Windows file system will see this as two slashes, which may or may not be harmless--I'm not sure. – ajb Apr 25 '14 at 17:06
  • Like I said, using '/', '//' or '\\' doesn't change anything and is still interpreted as "c:/example.txt" so it's irrelevant – AJCol Apr 25 '14 at 17:12

2 Answers2

3

This code probably doesn't do what you want:

try
{
    testDirectory.getData("c://example.txt");
}

catch (Exception ex)
{
    new FileNotFoundException("File not found");
}

If you catch any exception, you run the constructor for a FileNotFoundException and then throw it away. Try doing this:

try
{
    testDirectory.getData("c://example.txt");
}
catch (Exception ex)
{
    ex.printStackTrace();
}
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
1

According to the javadoc for Scanner, nextLine() throws this exception when there is no more input. Your program seems to expect it to return null, but that's now how it works (unlike, say, BufferedReader which does return null at the end of the input). Use hasNextLine to make sure there's another line before using nextLine.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • hasNextLine() returns true although I was expecting it to return false which is strange... – AJCol Apr 25 '14 at 17:15
  • @AJCol You did use `hasNextLine` in the loop, right? Instead of just once? I'd expect `hasNextLine()` to return `true` the first N times and then return `false`, if your file has N lines in it. – ajb Apr 25 '14 at 17:17
  • I have just printed out .next() which actually prints the first word in the file so I guess it doesn't have anything to do with the file. Thankyou for steering me in the right direction! – AJCol Apr 25 '14 at 17:18