1、如何找到當(dāng)前java類的路徑 (LoadProperties是自定義的類)
10年積累的網(wǎng)站建設(shè)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有舟山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
①、LoadProperties.class.getResource(""):返回當(dāng)前類LoadProperties.class文件的URI目錄。不包括自己!
②、 得到的是當(dāng)前的classpath的絕對(duì)URI路徑。(以下四種方式皆是)
LoadProperties.class.getResource("/");
Thread.currentThread().getContextClassLoader().getResource("");
LoadProperties.class.getClassLoader().getResource("");
ClassLoader.getSystemResource("");
③、得到當(dāng)前文件的系統(tǒng)路徑 (以下兩種皆是)
System.getProperty("user.dir");
new File("").getAbsolutePath()
/**
* 顯示當(dāng)前文件的路勁
*/
public static void printCurrentDir(){
// 得到的是當(dāng)前類FileTest.class文件的URI目錄。不包括自己!
System.out.println(LoadProperties.class.getResource(""));
// 得到的是當(dāng)前的classpath的絕對(duì)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(""));
// 得到當(dāng)前文件的路徑
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("").getAbsolutePath());
}
2、讀取properties文件
properties文件往往是用于存儲(chǔ)一些適合保存在本地的信息,這些信息不適合用作靜態(tài)變量,原因如果修改
這些變量,都會(huì)重新編譯執(zhí)行,浪費(fèi)時(shí)間。很多應(yīng)用都用到了這種文件來(lái)保存本地信息,如struts.properties,
在國(guó)際化處理中,也常用到xx_zh.properties..等。
如何讀取呢?
①、使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法,即通過(guò)系統(tǒng)類加載器加載到inputstream:
InputStream is = ClassLoader.getSystemResourceAsStream(path);
②、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法,即通過(guò)當(dāng)前類的類加載器加載到inputstream
InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream(path);
③、使用class變量的getResourceAsStream()方法,
InputStream is =? LoadProperties.class.getResourceAsStream(path);
④、通過(guò)InputStream (文件流)方式直接讀入
InputStream is = new BufferedInputStream(new FileInputStream(path));
⑤、使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù),該方式還是通過(guò)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());// 第一個(gè)參數(shù)為包名+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在同級(jí)目錄
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()靜態(tài)方法
*/
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類同級(jí)目錄下
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();
}
}
/**
* 通過(guò)InputStream 方式讀入
*/
public static void loadByInputSrtream(){
try {
String path = LoadProperties.class.getResource("")+relative_path;
InputStream is = new BufferedInputStream(new FileInputStream(path.substring(6)));// “file:/” 的長(zhǎng)度是6
p.load(is);
print();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù)
*/
@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();
// 遍歷數(shù)據(jù)
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);
}
}
/**
* 顯示當(dāng)前文件的路勁
*/
public static void printCurrentDir(){
// 得到的是當(dāng)前類FileTest.class文件的URI目錄。不包括自己!
System.out.println(LoadProperties.class.getResource(""));
// 得到的是當(dāng)前的classpath的絕對(duì)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(""));
// 得到當(dāng)前文件的路徑
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}.