Learn more about file paths in Android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.