Use of java properties files

Source: Internet
Author: User

We often see properties files (including log4j and junit) in java programs. As a programmer who knows little about java, it is necessary to clarify this issue.

First of all, this file is often used as a resource file. maven and sbt Chinese source files usually have an independent src/java/resource folder. The resource file is generally obtained through getResouce and getResourceAsStream (corresponding to InputStream) obtain. for example:

Test. class. getResource ("file3.txt ")

Test. class. getResource ("/file3.txt ")

Test. class. getClassLoader. getResource ("/file3.txt ")

The parameter can be a relative path or an absolute path. The relative path is found under the corresponding class file (classpath). The absolute path is found at the top-level classloader layer.

To use the properties file, you must import java. util. Properties. For example, in scala shell, enter:

Import java. util. Properties

Val x = getClass. getResourceAsStream ("/a. properties ")

Val prop = new Properties

Prop. load (x)

Prop. getProperty ("a") ==> return null

Prop. getProperty ("IcisReport. contextPath") ==> return/IcisReport

The corresponding a. properties are as follows:

# IcisReport ip Address
IcisReport. server. ip = 192.168.3.143
# IcisReport Port
IcisReport. server. port = 8080
# IcisReport context path
IcisReport. contextPath =/IcisReport


Refer:

Java resource path Summary: http://enetor.iteye.com/blog/1125189

Http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html


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.