How the path is specified when a Java program reads a resource file

Source: Internet
Author: User

Java programs, there are two ways to read resource operations.

The first is directly through file. such as FileReader, BufferedReader and so on. The file directory is relative to project. If I create a new MyProject, then, I have a icons folder under MyProject, if you want to read icons inside, the path is a relative path such as "Icons/***.gif" can be.

The second is obtained by GetClass (). GetResource (""). The second method must ensure that the file is under the path included in the classpath. The General Classpath (a. classpath file that can view any Eclipse project) includes at least the following:

<?xml version= "1.0" encoding= "UTF-8"?>
<classpath>
<classpathentry kind= "src" path= "src"/>
<classpathentry kind= "Con" path= "org.eclipse.jdt.launching.jre_container/ Org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/jdk1.7.0_51 "/>
<classpathentry kind= "Output" path= "Bin"/>
</classpath〉

The src and bin are the default, so the second way, the resource file must be under both folders. So if there is a Property.ini file under the SRC folder, it can be obtained by getresource ("/property.ini"), Note that this must start with '/' .


Maven project, the default has a resource folder, and this folder is under the Classpath path, so generally put the resource files here, and then conveniently through the getresource () to get the content.


And for xxx=xxx This configuration information, Java specifically designed a class to handle, ResourceBundle. The general processing method is as follows,

ResourceBundle resource = Resourcebundle.getbundle ("Conf/a.txt");

propertyResourceBundle bundle = (propertyresourcebundle) bundle;

enumeration<string> keys = Bundle.keys ();

for (String Key:keys)

{

Solve Bundle.getstring (key)

}

How the path is specified when a Java program reads a resource file

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.