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();