AppSensei
AppSensei

Reputation: 8400

How to fix this java.io.FileNotFoundException?

I'm trying to load a .csv file in a program but for some reason, it's unable to find the file. Where should I place the file?

enter image description here

Console

enter image description here

Upvotes: 0

Views: 22718

Answers (4)

user17975364
user17975364

Reputation: 1

I tried with all the above mention solution, but it didn't work.. but i went to my project folder and delete the target and tried to compile the project again. it then worked successfully

Upvotes: 0

Konstantinos Margaritis
Konstantinos Margaritis

Reputation: 3247

The file is located in the src directory so in order to access it you should use

src/Elevator.csv

As long as files are located inside your project folder you can access them using relative paths.

For example if a file is located under the Elevator folder then you access the file by using only its filename.

Elevator.csv

A good principle when using additional files in your project is creating separate folders from the ones that the source files are located. So you could create a folder resources under the project folder and place your file there. You can access then the file by using

resources/Elevator.csv

Upvotes: 2

Sumit Desai
Sumit Desai

Reputation: 1760

the path which it is trying to read is surely not exact as the path in which that file is actually present.Try printing absolute path of that file and compare it with actual path of your file.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500525

It looks like the file is in the src directory... which almost certainly isn't the working directory you're running in.

Options:

  • Specify an absolute filename
  • Copy the file to your working directory
  • Change the working directory to src
  • Specify a relative filename, having worked out where the working directory is
  • Include it as a resource instead, and load it using Class.getResourceAsStream

Upvotes: 7

Related Questions