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

創(chuàng)建一個final類
可以兩種方式讀取test.properties配置文件
第一種:此方法可以寫配置文件的絕對路徑
InputStream is = new BufferedInputStream(new FileInputStream(new File("F:\\java\\idea-workspace\\javaDemoForTest\\src\\test.properties")));第二種:此時test.properties配置文件的路徑與包同級,在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 {
// 方法一:通過輸入緩沖流進(jìn)行讀取配置文件,文件可以傳入絕對路徑
//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;
}
}注意,上述在加載文件的時候
prop.load(new InputStreamReader(is,"utf-8"));
使用配置文件對應(yīng)的編碼格式,不然會出現(xiàn)亂碼現(xiàn)象
編寫測試類,打印靜態(tài)變量的值
public static void main(String[] args) {
System.out.println(ReadProperties.getId());
System.out.println(ReadProperties.getName());
System.out.println(ReadProperties.getAddress());
}
成功,沒有出現(xiàn)亂碼現(xiàn)象
java相對路徑與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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-plus 批量插入太慢的問題解決(提升插入性能)
公司使用的Mybatis-Plus操作SQL,用過Mybatis-Plus的小伙伴一定知道他有很多API提供給我們使用,但是批量插入大數(shù)據(jù)太慢應(yīng)該怎么解決,本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-11-11
Java使用HttpUtils實現(xiàn)發(fā)送HTTP請求
這篇文章主要介紹了Java使用HttpUtils實現(xiàn)發(fā)送HTTP請求,HTTP請求,在日常開發(fā)中,還是比較常見的,今天給大家分享HttpUtils如何使用,需要的朋友可以參考下2023-05-05
Hibernate的一對一,一對多/多對一關(guān)聯(lián)保存的實現(xiàn)
本文主要介紹了Hibernate的一對一,一對多/多對一關(guān)聯(lián)保存的實現(xiàn),文中通過示例代碼介紹的很詳細(xì),感興趣的可以了解一下2021-09-09
Java lambda list轉(zhuǎn)換map時,把多個參數(shù)拼接作為key操作
這篇文章主要介紹了Java lambda list轉(zhuǎn)換map時,把多個參數(shù)拼接作為key操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring中的@ExceptionHandler異常攔截器
這篇文章主要介紹了Spring中的@ExceptionHandler異常攔截器,Spring的@ExceptionHandler可以用來統(tǒng)一處理方法拋出的異常,給方法加上@ExceptionHandler注解,這個方法就會處理類中其他方法拋出的異常,需要的朋友可以參考下2024-01-01

