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<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;
}
}
JAVA读取属性文件的几种方法相关推荐
- Java读取xml文件的四种方法
xml文件: Xml代码 <?xml version="1.0" encoding="GB2312"?><RESULT><VALU ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- java读取clob字段的几种方法
java读取clob字段的几种方法 讲道理,以前压根就没发现数据库中的clob字段和别的字段有什么区别,直到今天一下整出了一点小毛病,才去认真研究了一下. CLOB与BLOB的区别: BLOB和CLO ...
- matlab中读文件的行数_Matlab中读取txt文件的几种方法
Matlab中读取txt文件的几种方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如test.txt文件,内容为"17.901 -1.1 ...
- php远程读取几行文件,PHP读取远程文件的三种方法
PHP读取远程文件的三种方法 (2008-08-01 14:29:55) 标签: php 下载远程文件 it HP读取远程文件的几种方法,做采集的第一步就是读取远程文件- 1.file_get_con ...
- java读取csv文件的两种方式
java读取csv文件的两种方式 1.CsvReader读取 import com.csvreader.CsvReader; /*** CsvReader 读取* @param filePath* @ ...
- matlab读取cvs文件的几种方法
matlab读取CVS文件的几种方法: 1,实用csvread()函数 csvread()函数有三种使用方法: 1.M = csvread('filename') 2.M = csvread('fil ...
- matlab中如何读写txt,Matlab中读取txt文件的几种方法
Matlab中读取txt文件的几种方法 matlab读取文本文件的几种函数: 1.load--适合读取纯数据文本: 2.importdata--只读取数据,自动省略数据格式前后的字符,超大文件不适合: ...
- 静态变量读取属性文件_一种通过变量插值读取属性的方法
静态变量读取属性文件 最近,我尝试在应用程序服务器中定义和读取全局属性. 在应用程序服务器中配置的此类属性的好处–可以在此服务器上部署的所有Web应用程序之间共享该属性. 每个部署的应用程序都可以读取 ...
最新文章
- 跨浏览器图像灰度(grayscale)解决方案
- powerdns mysql_安装PowerDNS(使用MySQL后端)和Poweradmin在Debian Lenny
- python解析html的库_用python解析html
- 修改 input 框里的字体、颜色
- php伪静态后301,动态地址rewrite伪静态,然后301跳转到伪静态时死
- html5canvas简单画图
- 三层结构中的数据层设计
- java继承,final,super,Object类,toString,equals,
- Java随机产生中文昵称
- 苹果屏蔽更新描述文件_iOS屏蔽更新描述文件以及超级详细安装方法分享
- Qt----Qt控制LED
- 对bam文件作基础统计
- 《unix环境高级编程》--- 终端I/O
- android studio distributionurl是干嘛的,不懂就学系列(一):gradle配置本地distributionUrl...
- 维度建模和范式建模对比
- 神马笔记 版本2.0.0——新的旅程
- 标签ul与ol的区别及使用方法
- 中国最强AI超级服务器问世,每秒提供AI计算2000万亿次
- 计算机应用软件开机自动启动设置,电脑开机软件自动启动怎么关闭 win7/win10快速关闭开机自启软件...
- 深度学习——注意力机制
热门文章
- linux备份能压缩吗,Linux备份与压缩命令
- html5 mp4转换ogv格式,怎么将MP4转换为OGV?这么做超简单!
- 接口测试——Jmeter各部件执行顺序
- HTML示例06---段落(原格式标记)
- Spring Boot单元测试入门实战之关于JUnit
- mysql 把主键当外键_MySQL主键和外键使用及说明
- oracle+日誌語句,oracle维护常用sql语句
- upgrade cmake-gui version
- Farthest sampling on 3d mesh with mesh kept
- FCN数据预处理(code)