java properties配置文件操作

实现运用Java.util.Properties来进行对.properties配置文件操作。

配置文件实例:如debug.properties

#Tue Mar 21 15:46:17 CST 2017

#key=value

remote.debug.prot=7451

  1. 第一步写个获取文件路径的外部方法

    //-in- String filePath:配置文件名如debug.properties--

    //-return- 文件类对象--

public static File getFile (String filePath){

try{

ClassLoader classLoader = Thread.currentThread()

.getContextClassLoader();

URL url = classLoader.getResource(filePath);

return new File(url.toURI());

}catch(Exception e){

LOG.error("配置文件错误",e);

return null;

}

}

2.1根据key获取value值方法

//-in- String filePath:配置文件名如debug.properties--

//-in- key:键值如remote.debug.prot--

//-return- value值--

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

InputStream in = null;

try {

File fileURI = PropertiesUtil.getFile(filePath);

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

Properties pps = new Properties();

pps.load(in);

return pps.getProperty(key);

}catch (Exception e) {

LOG.error("初始化配置文件失败",e);

return null;

}finally{

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.2得到所有HashMap<key,value>方法

//-in- String filePath:配置文件名如debug.properties--

//-return-  HashMap<String, String>: HashMap<key, value>--

public static Map<String, String> getAllProperties(String filePath){

Map<String, String> proper = new HashMap<String, String>();

InputStream in = null;

try{

File fileURI = PropertiesUtil.getFile(filePath);

Properties pps = new Properties();

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

pps.load(in);

Enumeration<?> en = pps.propertyNames(); //得到配置文件的名字

while(en.hasMoreElements()) {

String strKey = (String) en.nextElement();

String strValue = pps.getProperty(strKey);

proper.put(strKey, strValue);

}

}catch(Exception e)

{

LOG.error("获取所有配置文件信息失败",e);

return null;

}finally{

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return proper;

}

2.3根据key获取value值方法

//-in- String filePath:配置文件名如debug.properties--

//-in- pKey:  键值如remote.debug.prot--

//-in- pValue: 结果值如 2000--

//-return- 状态值--

public static String setValueByKey (String filePath, String pKey, String pValue){

InputStream in = null;

OutputStream out = null;

try{

File fileURI = PropertiesUtil.getFile(filePath);

Properties pps = new Properties();

in = new FileInputStream(fileURI);

pps.load(in);

out = new FileOutputStream(fileURI);

pps.setProperty(pKey, pValue);

pps.store(out, "");

return TRUE;

}catch(Exception e)

{

LOG.error("插入配置文件失败", e);

return null;

}

finally{

try {

in.close();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.3写入所有key,value值方法

//-in- String filePath:配置文件名如debug.properties--

//-in- HashMap<String, String>:  键值对HashMap<key, value>如remote.debug.prot=900--

//-return- 状态值--

public static String setAllProperties(String filePath,Map<String, String> kv) {

InputStream in = null;

OutputStream out = null;

try{

File fileURI = PropertiesUtil.getFile(filePath);

Properties pps = new Properties();

in = new FileInputStream(fileURI);

pps.load(in);

out = new FileOutputStream(fileURI);

Iterator<Entry<String, String>> iter = kv.entrySet().iterator();

while (iter.hasNext()) {

@SuppressWarnings("rawtypes")

Map.Entry entry = (Map.Entry) iter.next();

Object key = entry.getKey();

Object val = entry.getValue();

pps.setProperty((String)key, (String)val);

}

pps.store(out, "");

}catch(Exception e)

{

LOG.error("插入配置文件失败", e);

return null;

}

finally{

try {

in.close();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return TRUE;

}

时间: 2024-12-15 06:50:49

java properties配置文件操作的相关文章

java对.properties配置文件操作

实现运用Java.util.Properties来进行对.properties配置文件操作. 配置文件实例:如debug.properties #Tue Mar 21 15:46:17 CST 2017 #key=value remote.debug.prot=7451 第一步写个获取文件路径的外部方法 //-in- String filePath:配置文件名如debug.properties-- //-return- 文件类对象-- public static File getFile (St

【Java】利用Java对配置文件操作实现Java程序的国际化

不仅仅是大名鼎鼎的Struts2,即便是小小的Java程序也能够实现国际化,根本就不用像网上大部分所说的那样,非西欧字符,例如我们的中文,岛国的日语之类的,必须使用native2ascii转码,坦诚,国际化字符串的配置文件xx_语言.propreties并不支持非西欧字符,只能转码存储,但你完全可以利用到我在<[Java]配置文件概念,Java对配置文件的操作>(点击打开链接)所介绍的方法,利用Java程序完成这个国际化的配置文件.Java会帮你自动转码. 一.基本目标 完成对大名鼎鼎的hel

软件开发工程师(JAVA)中级考试大纲-----四(四)Log4J的原理及配置;Log4J常用的API;在项目中应用日志框架Log4J关键类和接口介绍;Java properties配置文件log

log4j Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护进程等:我们也可以控制每一条日志的输出格式:通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程.最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码. 1定义 log4j--log for java(java的日志) 在强调可重用组件开发的今天,除了

Java properties配置文件

Java中的配置文件常为properties文件,格式为文本文件,文件内容的格式是"键=值"格式.注释信息使用"#"来注释. Properties类的常用方法 String getProperty(String key) 用指定的键在此属性列表中搜索属性.通过参数key得到其所对应的值 Object setProperty(String key,String value) 调用Hashtable的方法put. 通过调用基类的put()方法来设置键-值对 void l

java对properties配置文件的读写操作

1.1.  对properties配置文件的读取 1.1.1.  要读取的文件 配置文件key和value之间用冒号[:]和等于号[=]都是可以的. Test.properties name:\u5F20\u4E09 password:\u5BC6\u7801 age:22 1.1.2.  读取的程序代码 PropertiesTest.java /** * 读取配置文件 * @throws IOException */ @Test public void read() throws IOExce

properties配置文件读取操作总结【java笔记】

声明:本文所有例子中的 properties 文件均放在 src 目录下,ecclipse 软件自动增加 一.基本概念 1.1  properties文件,存储格式 键=值. properties文件特点: 1.键值对格式 2." = "等号后面,值前面,的空格,会自动忽略掉 3.值后面的空格,不会忽略 4." = "等号后面的双引号,不会忽略 5." # "井号后面内容,为注释,忽略 1.2 Java的 Properties 类 属性映射(pr

【001】java中配置文件properties的操作

properties文件在java的使用是比较常见的用于配置key-value的配置文件,java中也有专门对该种类型的文件进行处理的类Properties 一.Properties类API 1.Properties类描述 Properties继承了HashTable,明确的表示了它是一个key-value的集合,类中描述了可以使用get.put方法但是要保证key-value都是字符串否则会在写入文件时出错,也可以用其本身的getProperty.setProperty来操作这是安全的. 2.

JAVA使用和操作properties文件

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便.Properties 类存在于包 Java.util 中,该类继承自 Hashtable. 1. getProperty ( String  key) ,   用指定的键在此属性列表中搜索

java读取.properties配置文件的几种方法

读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put.putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的.因此java.util.Properties类提供了getProperty()和setPr