Java resource files

Source: Internet
Author: User

Java resource files

1.Overview

A resource file is also called a property file, which stores content in key = value format. You can use the properties class in the Code to read this file. String value = p. getProperty (key); to obtain the corresponding data. Generally, this file is stored as some parameters, and the code can be flexibly used to adapt to multi-language Environments. As the system's language environment changes, different attribute files can be read, xml can be used for configuration of complex points. In layman's terms, it is equivalent to defining a file, which defines the values of some variables and can be called in programs. The advantage is that if the parameter values in the program need to be changed, you can directly modify the. property file without modifying the source code. The advantage is that it is conducive to code refactoring in the future.

2.How to create a resource file

In IDE, a new file suffix is changed to. properties, and an attribute file is created.

3.Resource file structure

Attribute file (resource file) structure:

Test1 = aaa

Test2 = bbb

Example:

Driver = com. microsoft. jdbc. sqlserver. SQLServerDriver

Url = jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = books

User = sa

Password = 120010

4.Read resource files

To read the. properties file, follow these steps:

Create a class inherited from Properties and obtain the instance in singleton mode: Private Static Member, private default constructor, and public static synchronization method.

Use the getResourceAsStream () method of the Class Object to read the specified attribute file into the input stream and use the load () method in the Properties Class, read the attribute list (key/value pairs) from the input stream ).

Example:

The created. properties file is

jdbc.driver_class=org.gjt.mm.mysql.Driverjdbc.connection.url=jdbc\:mysql\://localhost\:3306/stumegjdbc.connection.username=rootjdbc.connection.password=1234

Create a tool class for reading configuration files:

Public class ConfigManager {private static ConfigManager configManager; // declare the configManager object private static Properties properties; // declare the properties object private ConfigManager () {// construction method without parameters // obtain the configuration file storage path String configFile = "database. properties "; // create properties object properties = new Properties (); // create a stream to obtain the configuration file content InputStream in = ConfigManager. class. getClassLoader (). getResourceAsStream (configFile); try {// call the load method properties. load (in);} catch (IOException e) {e. printStackTrace () ;}}// create Singleton mode public static ConfigManager getInstance () {if (configManager = null) {configManager = new ConfigManager ();} return configManager ;} public String getString (String key) {return properties. getProperty (key );}}

Then you need to use the local call

// Read the configuration information String driver = ConfigManager. getInstance (). getString ("jdbc. driver_class "); String url = ConfigManager. getInstance (). getString ("jdbc. connection. url "); String username = ConfigManager. getInstance (). getString ("jdbc. connection. username "); String password = ConfigManager. getInstance (). getString ("jdbc. connection. password ");




Java resource file

Property file refers to the properties File

Change the file suffix to. properties in IDE to create an attribute file.

Attribute file structure:
Test1 = aaa
Test2 = bbb
Ps: use unicode for Chinese Characters

There are many ways to read attribute files. One of my examples is:

// The path in the constructor is the *. properties file in the src folder.
// Assume that the property File a. properties is placed in the src folder.
ResourceBundle rb = ResourceBundle. getBundle ("");
String str = rb. getString ("test1 ");

The str value is the value corresponding to test1 in the property file.

What is a resource file in java?

Some resource files, clips, music, and so on are called in your code.
Reference: www.enet.com.cn/....shtml

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.