java修改.properties文件_Java读取,修改properties文件

package com.common.util;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.Properties;

/**

* 类名称 : PropsUtils

* 类描述 : 用于更新读取*.properties文件,

* 特别说明:为方便读,请使用getDefaultKeyFromClasspath方法,读取classpath下的res.properties

*/

public class PropsUtils {

private static Properties properties;

private static PropsUtils proUtil;

static {

properties = new Properties();

proUtil = new PropsUtils();

}

public PropsUtils() {

}

/**

* initStream

*

* @return

*/

public static Properties initStream(InputStream in, String... file)

throws Exception {

FileInputStream inputFile;

if (file != null && file.length == 1) {

inputFile = new FileInputStream(file[0]);

properties.load(inputFile);

} else {

properties.load(in);

}

return properties;

}

/**

*

 
 

* 从classpath得到Properties中的值

* @param key 要读取的key

* @param pathAndName 要读的文件(含路径),

* eg:getKeyFromClasspath(key,"res.properties")

* 或getKeyFromClasspath(key,"com/utils/res.properties")

* @return value

*

*/

public static String getKeyFromClasspath(String key, String pathAndName) {

String value = null;

try {

InputStream in = proUtil.getClass().getClassLoader()

.getResourceAsStream(pathAndName);

Properties properties = initStream(in);

value = properties.getProperty(key);

} catch (Exception e) {

System.err.println("load file error , file---->[" + pathAndName

+ "]," + " error message: " + e.toString());

e.printStackTrace();

}

return value;

}

/**

* 默认从classpath下的res.properties得到Properties中的值

*

* @param key

* 要读取的key

* @param pathAndName

* 要读的文件(含路径)

* @return value

*/

public static String getDefaultKeyFromClasspath(String key) {

String pathAndName = "res.properties";

String value = null;

try {

InputStream in = proUtil.getClass().getClassLoader()

.getResourceAsStream(pathAndName);

Properties properties = initStream(in);

value = properties.getProperty(key);

} catch (Exception e) {

System.err.println("load file error , file---->[" + pathAndName

+ "]," + " error message: " + e.toString());

e.printStackTrace();

}

return value;

}

/**

*

 
 

* 从文件系统得到Properties中的值

* @param key 要读取的key

* @param pathAndName 要读的文件(含路径)

* eg: getKeyFromFileSystem(key,"d:/res.properties")

* @return value

*

*/

public static String getKeyFromFileSystem(String key, String pathAndName) {

String value = null;

try {

InputStream in = new FileInputStream(pathAndName);

Properties properties = initStream(in);

value = properties.getProperty(key);

} catch (Exception e) {

System.err.println("load file error , file---->[" + pathAndName

+ "]," + " error message: " + e.toString());

e.printStackTrace();

}

return value;

}

/**

* 在classpath改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,

* 当key不存在时,该key的值是value

*

* @param key

* 要存入的键

* @param value

* 要存入的值

* @param pathAndName

* 要存的文件(含路径)

*/

public static void setClasspathPropes(String key, String value,

String pathAndName) {

try {

InputStream in = proUtil.getClass().getClassLoader()

.getResourceAsStream(pathAndName);

Properties properties = initStream(in);

in.close();

FileOutputStream out = new FileOutputStream("src/" + pathAndName);

properties.setProperty(key, value);

properties.store(out, "update " + key + " value");

out.close();

} catch (Exception e) {

System.err.println("set property error , file---->[" + pathAndName

+ "]," + " error message: " + e.toString());

e.printStackTrace();

}

}

/**

* 在文件系统改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,

* 当key不存在时,该key的值是value

*

* @param key

* 要存入的键

* @param value

* 要存入的值

* @param pathAndName

* 要存的文件(含路径)

*/

public static void setFileSystemPropes(String key, String value,

String pathAndName) {

try {

InputStream in = new FileInputStream(pathAndName);

Properties properties = initStream(in);

in.close();

FileOutputStream out = new FileOutputStream(pathAndName);

properties.setProperty(key, value);

properties.store(out, "update " + key + " value");

out.close();

} catch (Exception e) {

System.err.println("set property error , file---->[" + pathAndName

+ "]," + " error message: " + e.toString());

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值