简体   繁体   中英

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?

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM