java如何讀取properties文件將參數(shù)值配置到靜態(tài)變量
java讀取properties將參數(shù)值配置到靜態(tài)變量
將test.properties的文件讀取賦值給靜態(tài)變量

創(chuàng)建一個(gè)final類(lèi)
可以?xún)煞N方式讀取test.properties配置文件
第一種:此方法可以寫(xiě)配置文件的絕對(duì)路徑
InputStream is = new BufferedInputStream(new FileInputStream(new File("F:\\java\\idea-workspace\\javaDemoForTest\\src\\test.properties")));第二種:此時(shí)test.properties配置文件的路徑與包同級(jí),在src目錄下
InputStream is = ReadProperties.class.getClassLoader().getResourceAsStream("test.properties");package com.gangye;
import java.io.*;
import java.util.Properties;
/**
* @Classname ReadProperties
* @Description 讀取文件到靜態(tài)變量
* @Date 2020/7/9 17:03
* @Created by gangye
*/
public final class ReadProperties {
private static Integer id;
private static String name;
private static String address;
static {
Properties prop = new Properties();
try {
// 方法一:通過(guò)輸入緩沖流進(jìn)行讀取配置文件,文件可以傳入絕對(duì)路徑
//InputStream is = new BufferedInputStream(new FileInputStream(new File("F:\\java\\idea-workspace\\javaDemoForTest\\src\\test.properties")));
InputStream is = ReadProperties.class.getClassLoader().getResourceAsStream("test.properties");
// 傳入編碼格式解決中文亂碼
prop.load(new InputStreamReader(is,"utf-8"));
id = Integer.valueOf(prop.getProperty("id"));
name = prop.getProperty("name");
address = prop.getProperty("address");
} catch (IOException e) {
e.printStackTrace();
}
}
private ReadProperties(){}
public static Integer getId(){
return id;
}
public static String getName(){
return name;
}
public static String getAddress(){
return address;
}
}注意,上述在加載文件的時(shí)候
prop.load(new InputStreamReader(is,"utf-8"));
使用配置文件對(duì)應(yīng)的編碼格式,不然會(huì)出現(xiàn)亂碼現(xiàn)象
編寫(xiě)測(cè)試類(lèi),打印靜態(tài)變量的值
public static void main(String[] args) {
System.out.println(ReadProperties.getId());
System.out.println(ReadProperties.getName());
System.out.println(ReadProperties.getAddress());
}
成功,沒(méi)有出現(xiàn)亂碼現(xiàn)象
java相對(duì)路徑與properties文件讀取
// 打包jar同目錄下的application.properties,未打包 bin/application.properties
File file = new File(path + "application.properties");
Properties properties = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(file));
properties.load(in);
int port = Integer.parseInt(properties.getProperty("server.port"));
// 遍歷properties
// Iterator<String> it = properties.stringPropertyNames().iterator();
// while(it.hasNext()) {
// String key = it.next();
// System.out.println(key+":" + properties.getProperty(key));
//
// }總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-plus 批量插入太慢的問(wèn)題解決(提升插入性能)
公司使用的Mybatis-Plus操作SQL,用過(guò)Mybatis-Plus的小伙伴一定知道他有很多API提供給我們使用,但是批量插入大數(shù)據(jù)太慢應(yīng)該怎么解決,本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-11-11
Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求
這篇文章主要介紹了Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求,HTTP請(qǐng)求,在日常開(kāi)發(fā)中,還是比較常見(jiàn)的,今天給大家分享HttpUtils如何使用,需要的朋友可以參考下2023-05-05
Spring從@Aspect到Advisor使用演示實(shí)例
這篇文章主要介紹了Spring從@Aspect到Advisor使用演示實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
Hibernate的一對(duì)一,一對(duì)多/多對(duì)一關(guān)聯(lián)保存的實(shí)現(xiàn)
本文主要介紹了Hibernate的一對(duì)一,一對(duì)多/多對(duì)一關(guān)聯(lián)保存的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的很詳細(xì),感興趣的可以了解一下2021-09-09
Java lambda list轉(zhuǎn)換map時(shí),把多個(gè)參數(shù)拼接作為key操作
這篇文章主要介紹了Java lambda list轉(zhuǎn)換map時(shí),把多個(gè)參數(shù)拼接作為key操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
Spring中的@ExceptionHandler異常攔截器
這篇文章主要介紹了Spring中的@ExceptionHandler異常攔截器,Spring的@ExceptionHandler可以用來(lái)統(tǒng)一處理方法拋出的異常,給方法加上@ExceptionHandler注解,這個(gè)方法就會(huì)處理類(lèi)中其他方法拋出的異常,需要的朋友可以參考下2024-01-01

