-1

I am trying to use the opencsv library however I am getting stuck early on with FileReader not being able to find the csv I am using to test with.

I have the following code:

import java.io.File;
import java.io.FileReader;

public class Test {
public static void main(String[] args) {

    File f = new File("demo.csv");
    if(f.exists() && !f.isDirectory()) { 
        System.out.println("File exists");
    }
    else {
        System.out.println("File does not exist");
    }

    FileReader reader = new FileReader("demo.csv");
}
}

I am getting a FileNotFoundException error on the FileReader:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException at Test.main(Test.java:19)

despite having checked the file exists in the correct directory using f.exist. Done a load of searching and found nothing to explain it.

Can anyone help with this?

user207421
  • 305,947
  • 44
  • 307
  • 483
Josh
  • 189
  • 3
  • 12
  • Can you change your example into the bare minimum necessary to reproduce the error? So: dump the `while`-loop, add that `f.exists()` and eventually remove the dependency to opencsv – morido Feb 16 '16 at 10:11
  • @morido I have edited the OP how I believe you mean. Obviously the code as it stands doesn't run with the FileNotFoundException on the FileReader but if I comment that line out the f.exists returns "File exists" correctly. Even when I've tried to go to an absolute location (it's possible I've got the format wrong on that) it still can't find it. – Josh Feb 16 '16 at 19:39
  • 1. check your current working dir using `System.getProperty("user.dir");` - *demo.csv* must reside in that directory for your above code to work. 2. make sure the file permissions of `demo.csv` are correct, as this can also cause [FileNotFoundException](http://docs.oracle.com/javase/8/docs/api/java/io/FileNotFoundException.html) to be thrown. – morido Feb 16 '16 at 21:08
  • Please post the entire exception and message. At a guess you don't have read permission for the file, but without the exception text it is impossible to answer the question definitively. – user207421 Feb 17 '16 at 14:23
  • Checked working directory as instructed, file exists there and read/write permissions are set. Full error message is as follows, not much useful to add from it I don't think: `Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException at Test.main(Test.java:19)` At a loss on what else to try. At some point I'll see if I get the same on a different system. Could it be anything to do with FileReader not liking that particular csv file itself? I'll try another as well. – Josh Feb 17 '16 at 21:48
  • This is a *compiler error.* **Nothing to do** with not being able to find the file. You need to catch or throw the exception named. – user207421 Feb 17 '16 at 22:02

2 Answers2

0

check if the file extension is visible and your file is not named demo.csv.csv. Happend to me and took me a while to resolve.

mohitm
  • 153
  • 10
sebter
  • 46
  • 5
  • I thought that might be the case - already tried that, don't think that's the reason. Thanks for the suggestion though. – Josh Feb 16 '16 at 19:26
-1

By default, the compiler would look for "demo.csv" in the root directory. Make sure that you clearly specify the path in FileReader parameter. The same code does work in my case (provided demo.csv in right under the root directory)

mohitm
  • 153
  • 10
  • As I say, doing a f.exists check on it suggests the file exists. It's in where I believe the root directory should be. Is it possible FileReader might be looking elsewhere and not in the root directory? – Josh Feb 16 '16 at 19:26
  • 1
    *By default, the compiler would look for "demo.csv" in the root directory.* - This is **not true**. Neither is there any "compiler" involved here (hence, the existence of this file cannot be checked statically - which would not make much sense anyways) nor does the OS (think, Windows) necessarily have the notion of a "root directory". Thus, Java defaults to its [user.dir](http://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-it-means) property here. – morido Feb 16 '16 at 21:21