In the program code, how do I correctly access files in the project? For example, how do I set the parameters in getClass (). getResourceAsStream?
I have some information on the Internet, but it is not clear.
The source code is also simple.
1. First, no matter whether the jar or apk files are in zip format, the android internal search file is found in the zip package.
2. You can access file resources in the project through the relative path relative to the zip package root directory and relative path relative to the source file (. java ).
"/" Indicates the path relative to the root directory of the zip package (or the path relative to the classloader). If it is not started with "/", it indicates the path relative to the current source file.
In addition, the commonly used paths "." and "..." cannot be used.
Example
The directory in the. jar package is as follows:
A. jar
| ----- Com
| ----- Yy
| ----- Main. java
| -----Tt.txt
You can use the path through getclass().getresourceasstream(~~tt.txt) in main.java."/Com/yy/tt.txt"'Or"Tt.txt"Two methods.
However, if you use getClass (). getClassLoader (). getResourceAsStream (), you can only use"Com/yy/tt.txt"Because it is relative to the root directory of the zip package.
In addition, it should be noted that the project directory is different from the generated jar directory. For example, the source files are all under the source directory, but the source directory does not exist after the jar is exported, and only the package directory exists. If you are not sure, you can use zip to open the jar or apk file to view its actual directory structure.