一、如何找到當前java類的路徑 (LoadProperties是自定義的類)java
①、LoadProperties.class.getResource(""):返回當前類LoadProperties.class文件的URI目錄。不包括本身!ide
②、 獲得的是當前的classpath的絕對URI路徑。(如下四種方式皆是)函數
LoadProperties.class.getResource("/");spa
Thread.currentThread().getContextClassLoader().getResource("");orm
LoadProperties.class.getClassLoader().getResource("");get
ClassLoader.getSystemResource("");input
③、獲得當前文件的系統路徑 (如下兩種皆是)string
System.getProperty("user.dir");it
new File("").getAbsolutePath()io
/**
* 顯示當前文件的路勁
*/
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());
}
二、讀取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);
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());
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}.