开发时经常要读取properties文件的配置信息,但是properties文件所在的位置和properties访问方式不同读取方式也不同
1、访问方式一般分:java项目和web项目。
2、文件位置:与源文件相同目录和与源目录不相同
java项目与源文件相同目录读取properties文件方法,在main函数中读取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Properties;
public
class
PropertiesUtils {
public
static
void
main(String[] args) {
Properties properties =
new
Properties();
try
{
InputStream in = PropertiesUtils.
class
.getResourceAsStream(
"./db.properties"
);
//或者直接写成"db.properties"
properties.load(in);
String url = properties.getProperty(
"url"
);
String userName = properties.getProperty(
"user"
);
String password = properties.getProperty(
"password"
);
System.out.println(
"url="
+url+
",userName="
+userName+
",password="
+password);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
父级或子级目录只要加相应的方法路径即可。读取properties文件的关键是能够访问到properties文件,读取的方法是一样的,主要在于访问的方法不一样。
maven组织的web项目中读取properties文件方法
以下方法是在http请求情况下访问的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
Map<string, string=
""
> getDBInfo() {
Map<string, string=
""
> dbInfo =
new
HashMap<string, string=
""
>();
try
{
String url =
this
.getClass().getResource(
""
).getPath();
String path = url.substring(
0
, url.indexOf(
"WEB-INF"
)) +
"WEB-INF/db.properties"
;
Properties config =
new
Properties();
InputStream in =
new
FileInputStream(path);
config.load(in);
String dbUrl = config.getProperty(
"<span style="
font-family:Arial, Helvetica, sans-serif;
">dbUrl</span>"
);
dbInfo.put(
"url"
, dbUrl);
dbInfo.put(
"userName"
, config.getProperty(
"user"
));
dbInfo.put(
"password"
, config.getProperty(
"password"
));
}
catch
(Exception e) {
e.printStackTrace();
}
return
dbInfo;
}</string,></string,></string,>
|
以上两种区别主要在于getResourceAsStream和getResource。
附录:
Java中getResourceAsStream的用法:
首先,Java中的getResourceAsStream有以下几种:
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。
2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。
3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,当然这和具体的容器实现有关。
getResourceAsStream 用法大致有以下几种:
第一: 要加载的文件和.class文件在同一目录下,例如:com.demo 下有类Test.class ,同时有资源文件myfile.xml
那么,应该有如下代码:
Test.class.getResourceAsStream("myfile.xml");
第二:在me.class目录的子目录下,例如:com.demo 下有类Test.class ,同时在com.demo.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
Test.class.getResourceAsStream("file/myfile.xml");
或者如下写法:
Test.class.getResourceAsStream("./file/myfile.xml");
第三:不在Test.class目录下,也不在子目录下,例如:com.demo 下有类Test.class ,同时在 com.demo.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
Test.class.getResourceAsStream("/com/demo/file/myfile.xml");
总结一下,可能只是两种写法
第一:前面有 “/”
“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject
Test.class.getResourceAsStream("/com/demo/file/myfile.xml");
第二:前面没有 “/”,代表当前类的目录(当前路径也可以用“./”表示)
Test.class.getResourceAsStream("myfile.xml");
Test.class.getResourceAsStream("file/myfile.xml");
或者如下写法:
Test.class.getResourceAsStream("./myfile.xml");
Test.class.getResourceAsStream("./file/myfile.xml");