Whether you are a novice or a veteran, in the program to read the resource files will always encounter some files can not find the problem, which is related to the Java bottom of the implementation, not a bug, as long as the method is appropriate, the problem can be solved.
Folder structure for the project:
repathtest
├─src
│ └─com
│ └─lavasoft
│ ├─test
│ └─res
├─doc
1. Using relative paths in project in Java development tools
In project, the root directory of the relative path is the root folder of project, and this is the Repathtest folder.
The file is created by:
File f = new File("src/com/lavasoft/res/a.txt");
File f = new File("doc/b.txt");
Attention:
Path does not begin with "/";
Out of the IDE environment, this is wrong, not every IDE, but that's what I see.
2. Read package files through Classpath
Read the package file, the path must be a relative classpath path, such as a, located in the package, you can create a byte stream to read a:
InputStream in = ReadFile.class.getResourceAsStream ("/com/lavasoft/res/a.txt");
With a byte stream, you can read the contents of the file.
Attention:
This must begin with a "/";