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 { // 方法一:通過輸入緩沖流進行讀取配置文件,文件可以傳入絕對路徑 //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)文章
quartz的簡單使用、SpringBoot使用和自定義數(shù)據(jù)源集成方式
這篇文章主要介紹了quartz的簡單使用、SpringBoot使用和自定義數(shù)據(jù)源集成方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教<BR>2024-01-01關(guān)于spring?data?jpa?模糊查詢like的坑點
這篇文章主要介紹了關(guān)于spring?data?jpa?模糊查詢like的坑點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12java如何實現(xiàn)遞歸刪除樹形數(shù)據(jù)的任一個節(jié)點
文章講述了在Java中實現(xiàn)遞歸刪除樹形數(shù)據(jù)的任一個節(jié)點時需要注意的三個點,包括刪除的節(jié)點包含子節(jié)點、刪除子節(jié)點和其他子節(jié)點刪除的節(jié)點不包含子節(jié)點、以及該父節(jié)點變成葉子節(jié)點,此外,文章還提到這兩件事包含在同一件事務(wù)中2024-12-12詳解mybatis #{}和${}的區(qū)別、傳參、基本語法
這篇文章主要介紹了mybatis #{}和${}的區(qū)別、傳參、基本語法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07