java读取属性文件_JAVA读取属性文件的几种方法

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

示例:Java代码

InputStream in = lnew BufferedInputStream(new FileInputStream(name));

Properties p = new Properties();

p.load(in);

2.使用java.util.ResourceBundle类的getBundle()方法

示例:Java代码

ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3.使用java.util.PropertyResourceBundle类的构造函数

示例:Java代码

InputStream in = new BufferedInputStream(new FileInputStream(name));

ResourceBundle rb = new PropertyResourceBundle(in);

4.使用class变量的getResourceAsStream()方法

示例:ava代码

InputStream in = JProperties.class.getResourceAsStream(name);

Properties p = new Properties();

p.load(in);

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

示例:Java代码

InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);

Properties p = new Properties();

p.load(in);

6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

示例:Java代码

InputStream in = ClassLoader.getSystemResourceAsStream(name);

Properties p = new Properties();

p.load(in);

7.使用apache的PropertiesConfiguration类

示例:Java代码

Configuration config = new PropertiesConfiguration("test.properties");

config.getProperty(key);

补充Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

示例:Java代码

InputStream in = context.getResourceAsStream(path);

Properties p = new Properties();

p.load(in);

其中name为properties文件名字.但我在网上发现有人说要写properties文件的绝对路径,否则测试 不 能通过.我没验证过,有兴趣的朋友可以试试.

就我个人而言我是比较偏向用第3方法.我在网上找到一篇介绍的更为详细的文章,全文如下:在设计时,我们往往需要访问一些适合本地修改的配置信息,如果作为静态变量,那么每次修改都需要重新编译一个class,.config保存此类信息并不适合,这时我们需要ResourceBundle。通过ResourceBundle,我们需要访问位于/WEB-INF/classes目录下的一个后缀名为properties的文本类型文件,从里面读取我们需要的值。

Java代码

Locale locale = Locale.getDefault();

ResourceBundle localResource = ResourceBundle.getBundle("ConnResource", locale);

String value = localResource.getString("test");

System.out.println("ResourceBundle: " + value);

这里对应了/WEB-INF/class/ConnResource.properties文件内容为:

test=hello world

打印出来的结果就是hello world

请注意,这里我们可以利用Locale和ResourceBundle的这个组合创建国际化的java程序。我们可以把locale实例化为

Java代码

new Locale("zh","CN");

通过

Java代码

ResourceBundle.getBundle("MessagesBundle", locale);

系统将自动寻找MessagesBundle_zh_CN,即定义为中国大陆地区简体中文。如果没有该文件,则会依次寻找MessagesBundle_zh,MessagesBundle,直到找到为止。

/**

写入properties信息

@param filePath 绝对路径(包括文件名和后缀名)

@param parameterName 名称

@param parameterValue 值

*/

public static void writeProperties(String filePath,String parameterName,String parameterValue) {

Properties props = new Properties();

try {

//如果文件不存在,创建一个新的

File file=new File(filePath);

if(!file.exists()){

ToolKit.writeLog(Setting.class.getName(), "sharedata.properties 文件不存在,创建一个新的!"); file.createNewFile(); }

InputStream fis = new FileInputStream(filePath);

// 从输入流中读取属性列表(键和元素对)

props.load(fis);

fis.close();

OutputStream fos = new FileOutputStream(filePath);

props.setProperty(parameterName, parameterValue);

// 以适合使用 load 方法加载到 Properties 表中的格式,

// 将此 Properties 表中的属性列表(键和元素对)写入输出流

props.store(fos, parameterName);

fos.close(); // 关闭流 }

catch (IOException e) {

System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");

writeLog(This.class.getName(), "Visit "+filePath+" for updating "+parameterName+" value error", e); }

}

[代码] java读取属性(相对路径)

/*filename: 相对路径+文件名(不要后缀) */

public synchronized static String getPropertyFromFile(String filename, String key) {

ResourceBundle rb = ResourceBundle.getBundle(filename);

return rb.getString(key).trim(); }

/*

@Title: readValue

@Description: TODO 通过绝对路径获取properties文件属性, 根据key读取value

@param filePath properties文件绝对路径(包括文件名和后缀)

@param key 属性key

@return String 返回value

*/

public static String readValue(String filePath, String key){

Properties props = new Properties();

InputStream in=null;

try{

in = new BufferedInputStream(new FileInputStream(filePath));

props.load(in);

String value = props.getProperty(key);

return value; }

catch(Exception e){

e.printStackTrace();

return null;

}finally{

try {

in.close();//-----------------------------------important

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/**

本类主要是对config。properties的密码进行修改

@param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//写文件 String passwork = “123”;

//更改src的config包下的config.properties文件中的“userPassword”属性的值

writeProperties("config/config.properties","userPassword",passwork);

//config.properties一定要写完整

//从文件中取出userPassword,

String decStr=getPropertyFromFile("config/config", "userPassword");

System.out.println("============"+ decStr); }

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Properties;

import java.util.Set;

public class ParsePropertyFile {

public HashMap getProperty(String propertyFile) {

HashMap hm = null;

try {

Properties props = new Properties();

InputStream is = new FileInputStream(

new File(propertyFile).getAbsolutePath());

props.load(is);

Set keys = props.keySet();

hm = new HashMap();

for (Iterator it = keys.iterator(); it.hasNext();) {

String key = (String) it.next();

hm.put(key, props.getProperty(key));

}

is.close();

} catch (IOException ie) {

}

return hm;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值