Plantelo
Plantelo

Reputation: 1

Java can't open file?

I have the following code:

import java.IO.File;
...
File nameDataSource = new File("assets/names.txt");

It doesn't work. There is an assets subdirectory of the package/directory of the class. I tried putting the file next to the class file directly. I tried using an initial slash. I tried using an initial dot to represent current directory. All combination of these. None of them opened the file successfully. I don't want to use absolute paths because this is a Git project. What did I do wrong? How do I do it right?

Upvotes: 0

Views: 93

Answers (1)

The path must started from the project directory. If the file is in a subdirectory of the package, you should try: File nameDataSource = new File("src/package/assets/names.txt");.
To be sure of the path, take the absolute path and delite the begin. For example:

  • D:/Eclipse Workspace/project/assets/names.txt
  • assets/names.txt

If this won't work, write me again and we will try somethink else, and sorry for my bad english :)

Upvotes: 2

Related Questions