properties是一種屬性文件,這種文件以key=value格局存儲內容。Java中可以利用java.util.Properties類來讀取這個文件,按照左邊的key獲取值,String value=p.getProperty(key);
多種解析屬性文件的體例中java.util.Properties解析文件的體例是同一的,都是經由過程load加載InputStream 對象。本家兒如果獲取InputStream 對象的體例分歧,現實利用中我們紛歧心猿意馬能精確獲取properties屬性文件的絕對路徑。
屬性文件在eclipse和IDEA沒有安裝插件的環境下展示時會將中文轉為Unicode編碼,eclipse選中內容F2會顯示具體的中文內容(如下圖)。
第一種:本家兒如果經由過程class.getClassLoader().getResourceAsStream
1、是實現獲取在classpath路徑下的資本文件的輸入流
為什么是classpath而不是src,因為當web項目運行時,IDE編譯器會把src下的一些資本文件移至WEB-INF/classes,classPath目次其實就是這個classes目次。這個目次下放的一般是web項目運行時的class文件、資本文件(xml,properties...);
2、本家兒要代碼:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils {
/**
* 按照key獲取cfg.properties的值
*
* @param key
* @return
*/
public static String getCfgPropertiesValue(String key) {
Properties pro = new Properties();
InputStream is = null;
try {
is = PropertiesUtils.class.getClassLoader().getResourceAsStream(
"CodeMapping.properties");
System.out.println();
// 讀取屬性文件
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return pro.getProperty(key);
}
}
3、main方式測試成果
public class Test5 {
public static void main(String[] args) {
System.out.println("name:"
+ PropertiesUtils.getCfgPropertiesValue("name"));
}
}
第二種:經由過程class.getResourceAsStream(String name)獲取。
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test5 {
public static void main(String[] args) {
System.out.println("name:" + getCfgPropertiesValue("name"));
}
/**
* 按照key獲取CodeMapping.properties的值
* @param key
* @return
*/
public static String getCfgPropertiesValue(String key) {
Properties pro = new Properties();
InputStream is = null;
try {
is = Test5.class
.getResourceAsStream("/CodeMapping.properties");
System.out.println();
// 讀取屬性文件
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return pro.getProperty(key);
}
}
第三種:利用絕對路徑解析。
1、這種利用絕對路徑的體例需要你可以或許精確供給位置
2具體代碼如下所示:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test5 {
public static void main(String[] args) {
System.out
.println("name:"
+ getPropertiesValue(
"D:/Workspaces/MyEclipse 10/servlet/src/CodeMapping.properties",
"name"));
}
public static String getPropertiesValue(String filePath, String key) {
Properties pro = new Properties();
InputStream in = null;
try {
// 讀取屬性文件
in = new FileInputStream(filePath);
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return pro.getProperty(key);
}
}
第一步:javaweb項目打包總結。
1、通俗javaweb項目中的編譯會將內容編譯在WEB-INF\classes
1、springboot項目當地開辟會將src/main/java和resources目次下的內容編譯到target\classes下。打當作jar包之后:excelimport-0.0.1-SNAPSHOT.jar。目次為:excelimport-0.0.1-SNAPSHOT.jar\BOOT-INF\classes
第二步:class.getClassLoader().getResourceAsStream(file)和class.getResourceAsStream(file)比力。
1、都是實現獲取在classpath路徑下的資本文件的輸入流。
2、為什么是classpath而不是src,因為當web項目運行時,IDE編譯器會把src下的一些資本文件移至WEB-INF/classes,classPath目次其實就是這個classes目次。這個目次下放的一般是web項目運行時的class文件、資本文件(xml,properties...);
3、class.getClassLoader().getResourceAsStream(file)半斤八兩于直接在根目次classes查找文件和Thread.currentThread().getContextClassLoader().getResourceAsStream一樣,文件不在根目次classes下無法讀取。
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("CodeMapping.properties");
4、class.getResourceAsStream(file)定位相對于文件夾classes獲取下一級內容需要加“/”到具體目次
Test5.class.getResourceAsStream("/CodeMapping.properties");
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!