📄 gproperties.java
字号:
package g2w.app.gchm.lib;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
/**
* Extended properties.
*
* @author GreatGhoul
* @version 002 2009-3-10 23:01:26
*/
public class GProperties extends Properties {
File file = null;
FileInputStream is = null;
FileOutputStream os = null;
/**
* Contstructs a blank properties file.
*/
public GProperties() {
super();
}
/**
* Constructs a properties file using specified file path <i>path<i>,
* then automatically load it.
*
* @param path The path of the properties file.
* @throws IOException
*/
public GProperties(String path) throws IOException {
super();
file = new File(path);
load();
}
/**
* Loads the properties file using a proper parser.
*
* @throws IOException
*/
private void load() throws IOException {
if (file == null) {
throw new IOException("No file specifed");
} else if (file.exists()) {
is = new FileInputStream(file);
if (file.getName().toLowerCase().endsWith(".xml")) {
super.loadFromXML(is);
} else
super.load(is);
is.close();
}
}
/**
* Loads a new properties file using specified file path <i>path</i>.
*
* @param filename The specified path.
* @throws IOException
*/
public void load(String filename) throws IOException {
file = new File(filename);
// load();
}
/**
* 重载方法,识别文件类型,并采用相应的保存方式
* @param out 输出流
* @param comments 属性列表的描述
*/
public void store(OutputStream out, String comments) throws IOException {
if (file.getName().toLowerCase().endsWith(".xml"))
super.storeToXML(out, comments);
else
super.store(out, comments);
out.close();
}
/**
* 存储配置文件,不添加注释
* @throws IOException
*/
public void store() throws IOException {
if (file == null) {
throw new IOException("No file specified");
} if (os == null) {
os = new FileOutputStream(file);
store(os, null);
}
}
/**
* 存储配置文件,添加注释
* @param comments 注释
* @throws IOException
*/
public void store(String comments) throws IOException {
if (file == null) {
throw new IOException("No file specified");
} if (os == null) {
os = new FileOutputStream(file);
store(os, comments);
}
}
/**
* 以path为路径存储配置文件
* @param path 文件路径
* @throws IOException
*/
public void storeTo(String path) throws IOException {
load(path);
if (os == null)
os = new FileOutputStream(file);
store(os, null);
}
/**
* 以path为路径存储配置文件
* @param path 文件路径
* @throws IOException
*/
public void storeTo(String path, String comments) throws IOException {
load(path);
if (os == null)
os = new FileOutputStream(file);
store(os, comments);
}
/**
* 在屏幕上打印属性列表
*/
public void list() {
super.list(System.out);
}
public static void main(String[] args) {
try {
GProperties p = new GProperties();
p.setProperty("CurrentProjectPath", "C:\\Documents and Settings\\GreatGhoul\\My Documents\\chm demo\\");
p.list();
p.storeTo("config/config.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -