How to obtain a resource file under the project SRC directory

Source: Internet
Author: User

When we write Java applications, we may use some configuration files, such as config.properties, we can store some important information in the configuration file, such as the server's IP address, user name and password information.


In this article, we'll just discuss how to get the path to the resource file and not read the contents of the resource file.



1. Resources directory: SRC directory and Bin directory


As we write our Java code, we put the relevant resource files (for example, config.properties) into a package in the SRC directory and the SRC directory.


One thing to note, though, is that while we are placing the resource file in the SRC directory, when we read the resource file, it is in the bin directory (similar to SRC).


In other words, when we add a resource file, it is in the SRC directory, but when the program runs, the resources under the SRC directory are copied to the Bin directory, so the program accesses the resource files in the bin directory.


Before you make code access, let me do some preparation to prepare three files.

The first file is located in the Config.properties file under Com.rk.io, which reads as follows:

serverip=192.168.80.150username=parentdirectorypassword=123456

The second file is located in the Config.properties file under Com.rk.io.property, which reads as follows:

serverip=192.168.80.100username=currentdirectorypassword=123456

The third file is located in the Config.properties file under Com.rk.io.property.subdirectory, which reads as follows:

serverip=192.168.80.50username=subdirectorypassword=123456




2. Access to resources through the GetResource method of the Java.lang.Class class



2.1. How to access the path starting with "/"



2.1.1, accessing the first file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclassparent{public static void Main (string[] args) {URL ResourceUrl = PropertyClassParent.class.getResource ("/com/rk/io/config.properties"); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}

Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/config.properties



2.1.2, accessing a second file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclasscurrent{public static void Main (string[] args {URL ResourceUrl = PropertyClassCurrent.class.getResource ("/com/rk/io/property/config.properties"); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}


Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/property/config.properties



2.1.3, accessing a third file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclasssub{public static void Main (string[] args) { URL ResourceUrl = PropertyClassSub.class.getResource ("/com/rk/io/property/subdirectory/config.properties"); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}


Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/property/subdirectory/config.properties



2.2. How to access paths that do not start with "/"



2.2.1, accessing the first file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclassparent2{public static void Main (string[] args {URL ResourceUrl = PropertyClassParent2.class.getResource (".. /config.properties "); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}

Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/config.properties



2.2.2, accessing a second file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclassparent2{public static void Main (string[] args {URL ResourceUrl = PropertyClassParent2.class.getResource ("config.properties"); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}


Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/property/config.properties



2.2.3, accessing a third file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclasssub2{public static void Main (string[] args) { URL ResourceUrl = PropertyClassSub2.class.getResource ("subdirectory/config.properties"); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}

Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/property/subdirectory/config.properties



3. Access resources through Java.lang.ClassLoader's GetResource method


3.1. Access to the first file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclassparent{public static void Main (string[] args) {URL ResourceUrl = PropertyClassParent.class.getClassLoader (). GetResource ("com/rk/io/config.properties"); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}

Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/config.properties



3.2. Access to the second file

The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclasscurrent{public static void Main (string[] args {URL ResourceUrl = PropertyClassCurrent.class.getClassLoader (). GetResource ("Com/rk/io/property/config.properties "); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}

Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/property/config.properties



3.3. Access to a third file


The code is as follows:

Package Com.rk.io.property;import Java.net.url;public class Propertyclassloadersub{public static void Main (string[] args) {URL ResourceUrl = PropertyClassLoaderSub.class.getClassLoader (). GetResource ("com/rk/io/property/ Subdirectory/config.properties "); String fileName = Resourceurl.getfile (); System.out.println (FileName);}}

Output Result:

/d:/rupeng/workspace/rk/bin/com/rk/io/property/subdirectory/config.properties


4. Compare the GetResource methods of Java.lang.Class and Java.lang.ClassLoader

For the GetResource method of the Java.lang.Class class, in the passed in parameter:

If there is only one "/config.properties", it represents the Config.properties file under the bin directory;

If you enter "/com/rk/io/config.properties", it represents the Config.propertiesy file within Com/rk/io in the bin directory.

If you enter "Config.properties", it represents the config.properties in the same directory as the. class file of the current class.

If you enter ".. /config.properties ", represents the Config.properties file in the parent directory of the current class's. class.


For the GetResource method of Java.lang.ClassLoader, it is described in English as follows:

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can being accessed by class code in a-is independent of the Location of the code.


In this way, the location of the resource file and the class code are independent of each other (a resource is some the data that can was accessed by class code in a ways that is independent O f the location of the code)

How to obtain a resource file under the project SRC directory

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.