-2

I am trying to read a file from my desktop, but the following code always throws a FileNotFoundException:

try {
    Scanner fileIn = new Scanner(new FileReader("project.csv"));
} catch (FileNotFoundException e) {
    // The exception is always thrown.
}

How do I fix this?

cegredev
  • 1,485
  • 2
  • 11
  • 25
asap
  • 9
  • 4

1 Answers1

0

new FileReader("project.csv") tells the reader to search for the file in the current working directory, which is most likely your user folder. To get files on the desktop, do:

new FileReader("/desktop/project.csv")

But just to be safe, I would use the absolute path and include the one to the user directory as well:

new FileReader(System.getProperty("user.home") + "/desktop/project.csv")
cegredev
  • 1,485
  • 2
  • 11
  • 25