下面1-4的內(nèi)容是網(wǎng)上收集的相關(guān)知識(shí),總結(jié)來說,就是如下幾個(gè)知識(shí)點(diǎn):
1、最常用讀取properties文件的方法InputStream in = getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當(dāng)前類在同一文件夾下面。如果在不同的包中,必須使用:
1
|
InputStream ins = this .getClass().getResourceAsStream( "/cn/zhao/properties/testPropertiesPath2.properties" ); |
2、Java中獲取路徑方法
3、獲取路徑的一個(gè)簡單實(shí)現(xiàn)
4、反射方式獲取properties文件的三種方式
1 反射方式獲取properties文件最常用方法以及思考:
Java讀取properties文件的方法比較多,網(wǎng)上最多的文章是"Java讀取properties文件的六種方法",但在Java應(yīng)用中,最常用還是通過java.lang.Class類的getResourceAsStream(String name) 方法來實(shí)現(xiàn),但我見到眾多讀取properties文件的代碼中,都會(huì)這么干:
1
|
InputStream in = getClass().getResourceAsStream( "資源Name" ); |
這里面有個(gè)問題,就是getClass()調(diào)用的時(shí)候默認(rèn)省略了this!我們都知道,this是不能在static(靜態(tài))方法或者static塊中使用的,原因是static類型的方法或者代碼塊是屬于類本身的,不屬于某個(gè)對象,而this本身就代表當(dāng)前對象,而靜態(tài)方法或者塊調(diào)用的時(shí)候是不用初始化對象的。
問題是:假如我不想讓某個(gè)類有對象,那么我會(huì)將此類的默認(rèn)構(gòu)造方法設(shè)為私有,當(dāng)然也不會(huì)寫別的共有的構(gòu)造方法。并且我這個(gè)類是工具類,都是靜態(tài)的方法和變量,我要在靜態(tài)塊或者靜態(tài)方法中獲取properties文件,這個(gè)方法就行不通了。
那怎么辦呢?其實(shí)這個(gè)類就不是這么用的,他僅僅是需要獲取一個(gè)Class對象就可以了,那還不容易啊--
取所有類的父類Object,用Object.class難道不比你的用你正在寫類自身方便安全嗎 ?呵呵,下面給出一個(gè)例子,以方便交流。
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import java.util.Properties; import java.io.InputStream; import java.io.IOException; /** * 讀取Properties文件的例子 * File: TestProperties.java * User: leizhimin * Date: 2008-2-15 18:38:40 */ public final class TestProperties { private static String param1; private static String param2; static { Properties prop = new Properties(); InputStream in = Object. class .getResourceAsStream( "/test.properties" ); try { prop.load(in); param1 = prop.getProperty( "initYears1" ).trim(); param2 = prop.getProperty( "initYears2" ).trim(); } catch (IOException e) { e.printStackTrace(); } } /** * 私有構(gòu)造方法,不需要?jiǎng)?chuàng)建對象 */ private TestProperties() { } public static String getParam1() { return param1; } public static String getParam2() { return param2; } public static void main(String args[]){ System.out.println(getParam1()); System.out.println(getParam2()); } } |
運(yùn)行結(jié)果:
151
152
當(dāng)然,把Object.class換成int.class照樣行,呵呵,大家可以試試。
另外,如果是static方法或塊中讀取Properties文件,還有一種最保險(xiǎn)的方法,就是這個(gè)類的本身名字來直接獲取Class對象,比如本例中可寫成TestProperties.class,這樣做是最保險(xiǎn)的方法
2 獲取路徑的方式:
1
2
|
File fileB = new File( this .getClass().getResource( "" ).getPath()); System. out .println( "fileB path: " + fileB); |
2.2獲取當(dāng)前類所在的工程名:
1
|
System. out .println( "user.dir path: " + System. getProperty ( "user.dir" )) |
3 獲取路徑的一個(gè)簡單的Java實(shí)現(xiàn)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
/** *獲取項(xiàng)目的相對路徑下文件的絕對路徑 * * @param parentDir *目標(biāo)文件的父目錄,例如說,工程的目錄下,有l(wèi)ib與bin和conf目錄,那么程序運(yùn)行于lib or * bin,那么需要的配置文件卻是conf里面,則需要找到該配置文件的絕對路徑 * @param fileName *文件名 * @return一個(gè)絕對路徑 */ public static String getPath(String parentDir, String fileName) { String path = null ; String userdir = System.getProperty( "user.dir" ); String userdirName = new File(userdir).getName(); if (userdirName.equalsIgnoreCase( "lib" ) || userdirName.equalsIgnoreCase( "bin" )) { File newf = new File(userdir); File newp = new File(newf.getParent()); if (fileName.trim().equals( "" )) { path = newp.getPath() + File.separator + parentDir; } else { path = newp.getPath() + File.separator + parentDir + File.separator + fileName; } } else { if (fileName.trim().equals( "" )) { path = userdir + File.separator + parentDir; } else { path = userdir + File.separator + parentDir + File.separator + fileName; } } return path; } |
4 利用反射的方式獲取路徑:
1
2
3
|
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" ); InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" ); InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" ); |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/lanchengxiaoxiao/article/details/23776303