[英]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. class 的包/目录下有一个assets
子目录。 I tried putting the file next to the class file directly.我尝试将文件直接放在 class 文件旁边。 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.我不想使用绝对路径,因为这是一个 Git 项目。 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");
如果文件位于 package 的子目录中,您应该尝试: 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:例如:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.