当前位置:首页 » 编程语言 » java读取properties文件

java读取properties文件

发布时间: 2024-09-25 13:10:21

1. 在java中如何读取properties文件

最常用读取properties文件的方法
InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:
InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");
Java中获取路径方法
获取路径的一个简单实现
反射方式获取properties文件的三种方式

1 反射方式获取properties文件最常用方法以及思考:
Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:

InputStream in = getClass().getResourceAsStream("资源Name");

这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。

问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。

那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊--
取所有类的父类Object,用Object.class难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

/**
* 读取Properties文件的例子
* File: TestProperties.java
* User: leimin
* Date: 2008-2-15 18:38:40
*/
public final class TestProperties {
private static String param1;
private static String param2;

static {
Properties prop = new Properties();
InputStream in = Object. class .getResourceAsStream( "/test.properties" );
try {
prop.load(in);
param1 = prop.getProperty( "initYears1" ).trim();
param2 = prop.getProperty( "initYears2" ).trim();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 私有构造方法,不需要创建对象
*/
private TestProperties() {
}

public static String getParam1() {
return param1;
}

public static String getParam2() {
return param2;
}

public static void main(String args[]){
System.out.println(getParam1());
System.out.println(getParam2());
}
}

运行结果:

151
152
当然,把Object.class换成int.class照样行,呵呵,大家可以试试。

另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法
2 获取路径的方式:
File fileB = new File( this .getClass().getResource( "" ).getPath());

System. out .println( "fileB path: " + fileB);

2.2获取当前类所在的工程名:
System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span>
/**

*获取项目的相对路径下文件的绝对路径

*

* @param parentDir

*目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or

* bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径

* @param fileName

*文件名

* @return一个绝对路径

*/

public static String getPath(String parentDir, String fileName) {

String path = null;

String userdir = System.getProperty("user.dir");

String userdirName = new File(userdir).getName();

if (userdirName.equalsIgnoreCase("lib")

|| userdirName.equalsIgnoreCase("bin")) {

File newf = new File(userdir);

File newp = new File(newf.getParent());

if (fileName.trim().equals("")) {

path = newp.getPath() + File.separator + parentDir;

} else {

path = newp.getPath() + File.separator + parentDir

+ File.separator + fileName;

}

} else {

if (fileName.trim().equals("")) {

path = userdir + File.separator + parentDir;

} else {

path = userdir + File.separator + parentDir + File.separator

+ fileName;

}

}

return path;

}

4 利用反射的方式获取路径:
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" );

InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" );

InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );

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

3. JAVA中如何读取src下所有的properties文件

1.使用java.util.Properties类的load()方法


示例:

//文件在项目下。不是在包下!!

InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;

Properties p = new Properties();

p.load(in) ;

String className2 = p.getProperty("database.driver");

String url = p.getProperty("database.url");

String user = p.getProperty("database.user");

String password = p.getProperty("database.pass");

总结:如果是 在WEB上读取properties文件,写成下面这种。上面写的那些只在 JavaSE 中

String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

System.out.println(path);

InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));

Properties prop = new Properties();

4. java调用properties文件的问题

Java可使用Properties类读写properties,具体说明如下:

1.Properties类与Properties配置文件
Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

2.Properties中的主要方法
(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。
注释信息后面是属性文件的当前保存时间信息。
(3)getProperty/setProperty
这两个方法是分别是获取和设置属性信息。

3.代码实例
属性文件a.properties如下:
name=root
pass=liu
key=value
读取a.properties属性列表,与生成属性文件b.properties。代码如下:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
try {
// 读取属性文件a.properties
InputStream in = new BufferedInputStream(new FileInputStream("a.properties"));
// /加载属性列表
Properties prop = new Properties();
prop.load(in);
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + ":" + prop.getProperty(key));
}
in.close();
// /保存属性到b.properties文件
FileOutputStream oFile = new FileOutputStream("b.properties", true);// true表示追加打开
prop.setProperty("phone", "10086");
prop.store(oFile, "The New properties file");
oFile.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

热点内容
androidxml换行 发布:2024-09-25 15:05:59 浏览:113
plsql导出数据库备份 发布:2024-09-25 14:54:49 浏览:667
androidndkwindows 发布:2024-09-25 14:53:25 浏览:534
锐普数控权限密码是多少 发布:2024-09-25 14:53:12 浏览:944
泛型编程java 发布:2024-09-25 14:08:06 浏览:982
linux配置环境变量文件 发布:2024-09-25 13:58:49 浏览:265
备份集中的数据库备份与现有的不同 发布:2024-09-25 13:58:27 浏览:480
网络ip存储服务器 发布:2024-09-25 13:57:13 浏览:378
银行存储介质最终结果 发布:2024-09-25 13:55:41 浏览:111
linux显卡信息 发布:2024-09-25 13:28:36 浏览:552