import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 系统设置参数的读取和设置
* @author CodeToMyLaw
*
*/
public class SystemPropertiesUtils {
private static Properties properties = new Properties();
/**
* 初始化系统提供的参数
*/
private static void initProperties() {
InputStream in = null;
try {
String file = Thread.currentThread().getContextClassLoader().getResource("").getPath()+ "system.properties";
File f = new File(file);
in = new FileInputStream(f);
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 得到系统设置的参数
* @param key
* @return
*/
public static String getSystemParamValue(String key) {
initProperties();
String value = "";
if(properties != null) {
value = properties.getProperty(key);
}
return value;
}
/**
* 设置系统的参数
* @param key
* @param value
*/
public static void setSystemParamValue(String key,String value){
initProperties();
if(properties != null) {
properties.setProperty(key, value);
SystemPropertiesUtils s = new SystemPropertiesUtils();
String filename = s.getPath("system.properties");
try {
saveFile(filename,"Test");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 保存到配置文件中
*
* @param filename
* @param description
* @throws Exception
*/
public static void saveFile(String filename, String description) throws Exception {
try {
File f = new File(filename);
FileOutputStream fileOutputStream = new FileOutputStream(f);
properties.store(fileOutputStream, description);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException ex) {
throw new Exception("无法保存指定的配置文件:" + filename);
}
}
/**
* 获取配置文件的路径(此方法是放置到src的目录下)
*
* @param fileName
* @return
*/
public String getPath(String fileName) {
return getClass().getClassLoader().getResource("").getPath() + fileName;
}
}
分享到:
相关推荐
此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作
properties文件读写操作
c#操作properties,读写配置文件,非常方便,几行代码就搞定
简单的java操作properties文件
用JAVA轻松操作properties文件.txt
主要介绍了java项目中读取jdbc.properties文件操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
java操作properties文件
java操作Properties文件 获取项目当前目录 java操作Properties文件 获取项目当前目录 java操作Properties文件 获取项目当前目录 java操作Properties文件 获取项目当前目录
myeclipse 6.0.1 properties文件文中乱码问题详细解决: 1。包括各个版本eclipse的插件;插件放置位置;使用方法,(其实都很简单,但缺一不可,害我忙了大半天) 2。 native2ascii 手动转化的 最简单易用示例 3。 ...
java对properties文件的操作.pdf
详细介绍了java对properties文件的操作,并有案例介绍
查看properties 属性文件的插件。附有操作步骤。
在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,有很多如系统配置信息,java如何遍历properties文件呢,本文将详细介绍,希望可以帮助到您
在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们...JDK 内置的java.util.Properties 类为我们操作.properties 文件提供了便利。
JAVA操作properties文件.pdf
java操作config.properties方法,提供了操作txt、xml、properties文件读写方法