java:读/写配置文件

package jimmy;

import java.io.*;
import java.util.Properties; public class Program {
public static void main(String[] args) { String appTitle = "app_title";
String appVersion = "app_version";
String appProperties = "app.properties"; Properties settings = new Properties();
settings.put(appTitle, "应用程序标题");
settings.put(appVersion, "1.0"); try { //写入配置
FileOutputStream out = new FileOutputStream(appProperties);
settings.store(out, "AppConfig"); //读取配置
FileInputStream in = new FileInputStream(appProperties);
settings.load(in); String title = settings.getProperty(appTitle);
String version = settings.getProperty(appVersion); System.out.println(title + "\t" + version); } catch (Exception e) {
e.printStackTrace();
} }
}

上述代码将在应用根目录下生成配置:app.properties,内容如下:

#AppConfig
#Thu Nov 28 20:29:08 CST 2013
app_title=\u5E94\u7528\u7A0B\u5E8F\u6807\u9898
app_version=1.0

上一篇:UML 类图快速入门


下一篇:短信接口API