可以直接通過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。