java零配置讀取配置文件

packageresources;
importjava.io.IOException;
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.Driver;
importjava.sql.SQLException;
importjava.util.Properties;
publicclassDbUtil{
(){
StringdriverClass=null;
StringjdbcUrl=null;
Stringuser=null;
Stringpassword=null;

//讀取類路徑下的jdbc.properties文件,我的配置文件放在src包下
InputStreamin=DbUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
Propertiesproperties=newProperties();
try{
properties.load(in);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("username");
password=properties.getProperty("password");

Driverdriver=null;
try{
driver=(Driver)Class.forName(driverClass).newInstance();
}catch(InstantiationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

Propertiesinfo=newProperties();
info.put("user",user);
info.put("password",password);

//通過Driver的connect方法獲取資料庫連接.
Connectionconnection=null;
try{
connection=driver.connect(jdbcUrl,info);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println("----Thedatabaseisconnected----");
returnconnection;
}
}


jdbc.properties內容如下:
driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc:oracle:thin:@10.91.4.102:1521:orcljdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=cp2
password=cp2test


//調用方法
publicstaticvoidmain(String[]args){
Stringid="111111";
Stringgread="3";
List<Student>sList=newArrayList<Student>();
Connectioncon=DbUtilCR.getConnection();
PreparedStatementpre=null;
ResultSetresult=null;
Stringsql="selects.name,s.agefromstudentswheres.id=?ands.gread=?";
try{
pre=con.prepareStatement(sql);
pre.setString(1,id);//傳參數學號
pre.setString(2,gread);//傳參數年級
result=pre.executeQuery();
System.out.println("執行SQL為:["+sql+"]");
System.out.println("參數為:["+id+","+gread+"]");
while(result.next()){
Studentst=newStudent();
st.setName(result.getString("name"));//與查詢出的欄位或者別名保持一致
st.setAge(result.getString("age"));
sList.add(st);
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
for(inti=0;i<sList.size();i++){
System.out.println("姓名:"+sList.get(i).getName()+" 年齡:"+sList.get(i).getAge());

}
}

Ⅱ 如何在java類中讀取Properties配置文件

最常用讀取properties文件的方法 InputStream in = getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當前類在同一文件夾下面。如果在不同的包中,必須使用: InputStream ins = this.getClass().getResourceAsStream(

Ⅲ java中用Properties類載入配置文件

一個Properties只能載入一個問題,如果你需要載入多個的話只能多回寫幾個了。
例如答:
Properties prop = new Properties();
prop.load(ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties"));

Properties prop1 = new Properties();
prop1.load(ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties1"));

Ⅳ 如何使用java類來載入properties配置文件的屬性信息

一個Properties只能載入一個問題,如果你需要載入多個的話只能多寫幾個了。例如:Propertiesprop=newProperties();prop.load(ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties"));Propertiesprop1=newPro

Ⅳ java怎樣提取配置文件

java讀取配置文件的幾種方法如下:
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式載入資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能載入類classes下面的資源文件且只能讀取.properties文件。
方式三:採用ClassLoader方式進行讀取配置信息
優點是:可以在非Web應用中讀取配置資源信息,可以讀取任意的資源文件信息
缺點:只能載入類classes下面的資源文件。
方法4 getResouceAsStream
XmlParserHandler.class.getResourceAsStream 與classloader不同
使用的是當前類的相對路徑

Ⅵ Java中如何設置讀取ini配置文件

/*
* IniReader.java
* 用Java讀取INI文件(帶section的)
* 示例:
* IniReader reader = new IniReader("E:\\james\\win.ini");
* out.println(reader.getValue("TestSect3", "kkk 6"));
*/

package tmp;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public class IniReader {

protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;

public IniReader(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}

protected void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}

protected void parseLine(String line) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new Properties();
sections.put(currentSecion, current);
} else if (line.matches(".*=.*")) {
if (current != null) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name, value);
}
}
}

public String getValue(String section, String name) {
Properties p = (Properties) sections.get(section);

if (p == null) {
return null;
}

String value = p.getProperty(name);
return value;
}

}

ini文件如下:

[TestSect1]
kkk 1=VVVVVVVVVVV1
kkk 2=VVVVVVVVVVV2

[TestSect2]
kkk 3=VVVVVVVVVVV3
kkk 4=VVVVVVVVVVV4

[TestSect3]
kkk 5=VVVVVVVVVVV5
kkk 6=VVVVVVVVVVV6

Ⅶ java 怎麼讀取配置文件

一.讀取xml配置文件
(一)新建一個java bean(HelloBean. java)

java代碼
(二)構造一個配置文件(beanConfig.xml)
xml 代碼
(三)讀取xml文件
1.利用

java代碼
2.利用FileSystemResource讀取
java代碼
二.讀取properties配置文件
這里介紹兩種技術:利用spring讀取properties 文件和利用java.util.Properties讀取
(一)利用spring讀取properties 文件
我們還利用上面的HelloBean. java文件,構造如下beanConfig.properties文件:
properties 代碼
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
屬性文件中的"helloBean"名稱即是Bean的別名設定,.class用於指定類來源。
然後利用org.springframework.beans.factory.support.來讀取屬性文件

java代碼

(二)利用java.util.Properties讀取屬性文件
比如,我們構造一個ipConfig.properties來保存伺服器ip地址和埠,如:
properties 代碼
ip=192.168.0.1
port=8080
三.讀取位於Jar包之外的properties配置文件

下面僅僅是列出讀取文件的過程,剩下的解析成為properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");

2 num = reader.read(byteStream);

3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);

四.要讀取的配置文件和類文件一起打包到一個Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //獲取當前Jar文件名,並對其解碼,防止出現中文亂碼
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名

修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //寫配置文件
。。。

out.close();

Ⅷ Java,載入不到配置文件中的值

建議做下面兩個檢查,1.檢查,配置文件是否加到編譯的classpath下,2.檢查文件的載入路徑是否有問題,如果是包外配置,是否有盤符的問題

Ⅸ Java讀取配置文件的幾種方法以及路徑問題

.類載入器讀取:
只能讀取classes或者類路徑中的任意資源,但是不適合讀取特別大的資源。
①獲取類載入器 ClassLoader cl = 類名.class.getClassLoader();
②調用類載入器對象的方法:public URL getResource(String name);
此方法查找具有給定名稱的資源,資源的搜索路徑是虛擬機的內置類載入器的路徑。
類 URL 代表一個統一資源定位符,它是指向互聯網」資源」的指針。
資源可以是簡單的文件或目錄,也可以是對更為復雜的對象的引用.
URL對象方法:public String getPath(),獲取此 URL 的路徑部分。
示例代碼:
2.類載入器讀取:
只能讀取classes或者類路徑中的任意資源,但是不適合讀取特別大的資源。
①獲取類載入器 ClassLoader cl = 類名.class.getClassLoader();
②調用類載入器對象的方法:public InputStream getResourceAsStream(String name);
返回讀取指定資源的輸入流。資源的搜索路徑是虛擬機的內置類載入器的路徑。

Ⅹ java讀取配置文件的方法(xml)

用的是jdom包

URL url = RederXml.class.getClassLoader().getResource("");
String path = url.toString() + "/config.xml";\\工程種xml的路徑
HashMap<String, String> map = new HashMap<String, String>();
SAXBuilder sax = new SAXBuilder();
Document doc = null;
try {
doc = sax.build(path);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element root = doc.getRootElement();