您现在的位置是:首页 > 文章详情

JAVA读取属性文件的几种方法

日期:2018-10-07点击:294

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<String, String> getProperty(String propertyFile) { HashMap<String, String> hm = null; try { Properties props = new Properties(); InputStream is = new FileInputStream( new File(propertyFile).getAbsolutePath()); props.load(is); Set<Object> keys = props.keySet(); hm = new HashMap<String, String>(); for (Iterator<Object> it = keys.iterator(); it.hasNext();) { String key = (String) it.next(); hm.put(key, props.getProperty(key)); } is.close(); } catch (IOException ie) { } return hm; }

}

原文链接:https://yq.aliyun.com/articles/648077
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章