如何找到當前文件路徑和如何讀取properties文件

1、如何找到當前java類的路徑 (LoadProperties是自定義的類)


①、LoadProperties.class.getResource(""):返回當前類LoadProperties.class文件的URI目錄。不包括自己!


②、 得到的是當前的classpath的絕對URI路徑。(以下四種方式皆是)


LoadProperties.class.getResource("/");


Thread.currentThread().getContextClassLoader().getResource("");


LoadProperties.class.getClassLoader().getResource("");


ClassLoader.getSystemResource("");


③、得到當前文件的系統路徑 (以下兩種皆是)


System.getProperty("user.dir");


new File("").getAbsolutePath()


/**

* 顯示當前文件的路勁

*/

public static void printCurrentDir(){

// 得到的是當前類FileTest.class文件的URI目錄。不包括自己!

System.out.println(LoadProperties.class.getResource(""));

// 得到的是當前的classpath的絕對URI路徑。(以下四種方式皆是)

System.out.println(LoadProperties.class.getResource("/"));

System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));

System.out.println(LoadProperties.class.getClassLoader().getResource(""));

System.out.println(ClassLoader.getSystemResource(""));

// 得到當前文件的路徑

System.out.println(System.getProperty("user.dir"));

System.out.println(new File("").getAbsolutePath());

}





2、讀取properties文件


properties文件往往是用於存儲一些適合保存在本地的信息,這些信息不適合用作靜態變量,原因如果修改


這些變量,都會重新編譯執行,浪費時間。很多應用都用到了這種文件來保存本地信息,如struts.properties,


在國際化處理中,也常用到xx_zh.properties..等。


如何讀取呢?


①、使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法,即通過系統類加載器加載到inputstream:


InputStream is = ClassLoader.getSystemResourceAsStream(path);


②、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法,即通過當前類的類加載器加載到inputstream


InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream(path);


③、使用class變量的getResourceAsStream()方法,


InputStream is =  LoadProperties.class.getResourceAsStream(path);


④、通過InputStream (文件流)方式直接讀入


InputStream is = new BufferedInputStream(new FileInputStream(path));


⑤、使用java.util.PropertyResourceBundle類的構造函數,該方式還是通過inputstream流讀入的


InputStream is = new BufferedInputStream(new FileInputStream(path));


ResourceBundle rb = new PropertyResourceBundle(is);


⑥、使用java.util.ResourceBundle類的getBundle()方法


ResourceBundle rb = ResourceBundle.getBundle("Test201308/message",Locale.getDefault());// 第一個參數爲包名+properties文件的名字(不要加後綴)




代碼:


package Test201308;

 

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.text.MessageFormat;

import java.util.Iterator;

import java.util.Locale;

import java.util.Properties;

import java.util.PropertyResourceBundle;

import java.util.ResourceBundle;

 

public class LoadProperties {

// message.properties 和 LoadProperties.class在同級目錄

private static final String absolute_path = "Test201308/message.properties";

private static final String relative_path = "message.properties";

private static Properties p = new Properties();

private static Object key;

private static Object value;

/**

* 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法

*/

public static void loadBySystemResourceStream(){

String path = absolute_path;

InputStream is = ClassLoader.getSystemResourceAsStream(path);

try {

p.load(is);

print();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

*/

public static void loadByCurentClassLoader(){

InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream(absolute_path);//message.properties在LoadProperties類同級目錄下

try {

p.load(is);

print();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 使用class變量的getResourceAsStream()方法

*/

public static void loadByCurrentClass(){

InputStream is =  LoadProperties.class.getResourceAsStream(relative_path);

try {

p.load(is);

print();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 通過InputStream 方式讀入

*/

public static void loadByInputSrtream(){

try {

String path = LoadProperties.class.getResource("")+relative_path;

InputStream is = new BufferedInputStream(new FileInputStream(path.substring(6)));// “file:/” 的長度是6

p.load(is);

print();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 使用java.util.PropertyResourceBundle類的構造函數

*/

@SuppressWarnings("rawtypes")

public static void loadByPropertyResourceBundle(){

String path = System.getProperty("user.dir")+"/bin/"+absolute_path;

try {

InputStream is = new BufferedInputStream(new FileInputStream(path));

ResourceBundle rb = new PropertyResourceBundle(is);

// print 

Iterator it = rb.keySet().iterator();

for(;it.hasNext();){

key = it.next();

value =  rb.getObject(key.toString());

value = MessageFormat.format(value.toString(), new Object[]{"sb","NCBC"});

System.out.println("key:"+key+" value:"+value);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 使用java.util.ResourceBundle類的getBundle()方法

*/

@SuppressWarnings("rawtypes")

public static void loadByResourceBundle(){

ResourceBundle rb = ResourceBundle.getBundle("Test201308/message",Locale.getDefault());

// print 

Iterator it = rb.keySet().iterator();

for(;it.hasNext();){

key = it.next();

value = rb.getString(key.toString());

value = MessageFormat.format(value.toString(), new Object[]{"sb","NCBC"});

System.out.println("key:"+key+" value:"+value);

}

}

/**

* 打印顯示

*/

@SuppressWarnings("rawtypes")

public static void print(){

Iterator keys = p.keySet().iterator();

// 遍歷數據

while(keys.hasNext()){

key = (String)keys.next();

value = p.getProperty(key.toString());

value= MessageFormat.format(value.toString(), new Object[]{"sb","NCBC"});

System.out.println("key:"+key+" value:"+value);

}

}

/**

* 顯示當前文件的路勁

*/

public static void printCurrentDir(){

// 得到的是當前類FileTest.class文件的URI目錄。不包括自己!

System.out.println(LoadProperties.class.getResource(""));

// 得到的是當前的classpath的絕對URI路徑。(以下四種方式皆是)

System.out.println(LoadProperties.class.getResource("/"));

System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));

System.out.println(LoadProperties.class.getClassLoader().getResource(""));

System.out.println(ClassLoader.getSystemResource(""));

// 得到當前文件的路徑

System.out.println(System.getProperty("user.dir"));

System.out.println(new File("").getAbsolutePath());

}

/**

* main 入口

* @param args

*/

public static void main(String[] args) {

printCurrentDir();

// 方式1 1

loadBySystemResourceStream();

// 方式2

loadByCurentClassLoader();

// 方式3 1

loadByCurrentClass();

// 方式4

loadByInputSrtream();

// 方式5

loadByPropertyResourceBundle();

// 方式6

loadByResourceBundle();

}

 

}


message.properties:

message = {0} says that message: {1}.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章