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