📄 propertiesunit.java
字号:
/*
* 创建日期 2006-6-1
*/
package com.tjsoft.xml.parse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author sun
*/
public class PropertiesUnit {
private String filename;
private Properties pros;
private FileInputStream in;
private FileOutputStream out;
public Properties getPros() {
if (pros == null) {
pros = new Properties();
}
return pros;
}
public PropertiesUnit(String filename) {
this.filename = filename;
File file = new File(filename);
try {
in = new FileInputStream(file);
getPros().load(in);
in.close();
} catch (FileNotFoundException e) {
System.err.println("配置文件config.properties找不到!");
e.printStackTrace();
} catch (IOException e) {
System.err.println("读取配置文件config.properties错误!");
e.printStackTrace();
}
}
public String getValue(String itemName) {
return getPros().getProperty(itemName);
}
public String getValue(String itemName, String defaultValue) {
return getPros().getProperty(itemName, defaultValue);
}
public void setValue(String itemName, String value) {
getPros().setProperty(itemName, value);
}
public void saveFile(String filename, String description) throws Exception {
try {
File f = new File(filename);
out = new FileOutputStream(f);
getPros().store(out, description);
out.close();
} catch (IOException ex) {
throw new Exception("无法保存指定的配置文件:" + filename);
}
}
public void saveFile(String filename) throws Exception {
saveFile(filename, "");
}
public void saveFile() throws Exception {
if (filename.length() == 0)
throw new Exception("需指定保存的配置文件名");
saveFile(filename);
}
public void deleteValue(String value) {
getPros().remove(value);
}
public static void main(String args[]) {
String file = PropertiesUnit.class.getResource(
"/database_config.properties").getPath();
PropertiesUnit pu = new PropertiesUnit(file);
System.out.println(pu.getPros());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -