導航:首頁 > 文件教程 > 讀入配置文件時路徑

讀入配置文件時路徑

發布時間:2023-04-08 22:51:15

java讀取properties配置文件路徑問題

可以直接通過Thread方法直接獲取到項目路徑下的配置文件:
static {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("BankConPort.properties"); //載入線程文件成為流
Properties prop = new Properties();
try {
prop.load(is);//直接轉換為對象
BOB_ReqURI = prop.getProperty("BOB_ReqURI");
BOB_SignURI = prop.getProperty("BOB_SignURI");
BOBLOGINPASSWORD = prop.getProperty("BOBLOGINPASSWORD");
} catch (IOException ex) {
java.util.logging.Logger.getLogger(BOBUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.info("解析信息出錯", e.getMessage());

}
}
}
}

㈡ java 程序打包為jar發布後,讀取配置文件路徑出錯 ,怎樣獲取配置文件路徑

給你個例子,讀取config.properties文件。
文件內容(值自己加)如下:
TestHosts =
FormalHosts =

TestConfig =
FormalConfig =

HostsPath =
ConfigPath =

讀取文件的類如下:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class EvnConfig{
public static Properties PROPERTIES = new Properties();

static{
String proFilePath = System.getProperty("user.dir")+"/config.properties";
//System.out.println(proFilePath);
//InputStream propertiesStream = EvnConfig.class.getClassLoader().getResourceAsStream(proFilePath);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(proFilePath));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
PROPERTIES.load(in);
}catch(IOException e){
System.out.println("properties創建失敗!");
e.printStackTrace();
}
//System.out.println("EvnConfig.testHosts:"+PROPERTIES.getProperty("TestHosts"));
}

public static final String testHosts = changeCode(PROPERTIES.getProperty("TestHosts"));
public static final String formalHosts = changeCode(PROPERTIES.getProperty("FormalHosts"));
public static final String testConfig = changeCode(PROPERTIES.getProperty("TestConfig"));
public static final String formalConfig = changeCode(PROPERTIES.getProperty("FormalConfig"));
public static final String hostsPath = changeCode(PROPERTIES.getProperty("HostsPath"));
public static final String configPath = changeCode(PROPERTIES.getProperty("ConfigPath"));

public static String changeCode(String str){
String toStr = "";
try {
//System.out.println(str + "轉換...");
toStr = new String(str.getBytes("ISO-8859-1"),"GB2312");
//System.out.println(str + "轉換成功!");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(str + "轉換失敗!");
e.printStackTrace();
}
return toStr;

}
}

㈢ idea jvm option的配置文件的讀取路徑

在這個裡面的配置文件和讀取路徑的話是通過他的官方網站上去讀取的,你可以去他的優盤裡面去看一下。

㈣ cmd 運行jar包讀取配置文件時路徑報錯

你是怎麼打包的?手工用 WinRAR/WinZip/7Zip 之類的軟體打開這個 jar 文件確認一下,那個 xml 文件在什麼位置。

一般推薦的做法是把配置文件集中放在 /META-INF 文件夾中。

㈤ jar包中的類讀取配置文件的路徑問題,求大家幫幫忙

應該是直接就可以讀取的。

因為你的配置文件和Jar文件在同一個目錄下,如果你的應用能找到jar文件,也就是可以找到配置文件。不過你要在classpath路徑中添加*.properties。

閱讀全文

與讀入配置文件時路徑相關的資料

熱點內容
javahashmapvalues 瀏覽:548
滑鼠選定不了文件內容是壞了么 瀏覽:589
打開excel標准模板文件名 瀏覽:798
該文件名字不包含擴展名 瀏覽:565
華為備份文件 瀏覽:368
批量刪除cpp文件代碼注釋的工具 瀏覽:312
下列哪些不屬於可編程邏輯器件 瀏覽:963
蘋果6p跳屏是什麼原因 瀏覽:383
下載文件路徑是什麼 瀏覽:852
linux下o文件多重定義 瀏覽:135
為什麼在人多的地方沒有網路 瀏覽:170
華為g7有多少個版本 瀏覽:949
實名寶app哪個好 瀏覽:1
微雲單個文件可以傳多少 瀏覽:843
計算機連成網路的最重要優勢是 瀏覽:411
優盤打開後文件夾為空 瀏覽:495
實時數據寫入量大如何優化 瀏覽:76
哪裡能學程序編程 瀏覽:647
微信裡面的文件儲存在哪個目錄 瀏覽:745
高仿蘋果5s屏幕顯示清楚嗎 瀏覽:897

友情鏈接