- 程序启动
- 判断程序运行操作系统类型
- 根据操作系统类型去执行对应操作系统的方法
- 判断win c盘中appdata中指定的文件夹是否存在,如果存在读入数据,如果不存在就在appdata路径中创建一个文件夹
创建配置文件
如果是windows系统,就直接将AppData目录作为配置文件的存放目录:
String roamingConfigHome = System.getenv("APPDATA");
if (roamingConfigHome != null) {
File roamingConfigHomeFile = new File(roamingConfigHome);
if (roamingConfigHomeFile.exists()) {
return new File(roamingConfigHomeFile, Constants.CONFIG_FILENAME);
}
}
此时roamingConfigHome的值为:C:\Users\erbin\AppData\Roaming
而配置文件的名称为:
public static final String CONFIG_FILENAME = "jd-gui.cfg";
将appdata作为用户目录,是windows桌面程序一个常见的做法
读取配置文件
org.jd.gui.service.configuration.ConfigurationXmlPersisterProvider
@Override
public Configuration load() {
// Default values
// 获取窗口默认的像素
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// 判断默认的像素与程序支持的(设置在类中的固定值)比较
int w = (screenSize.width>Constants.DEFAULT_WIDTH) ? Constants.DEFAULT_WIDTH : screenSize.width;
int h = (screenSize.height>Constants.DEFAULT_HEIGHT) ? Constants.DEFAULT_HEIGHT : screenSize.height;
int x = (screenSize.width-w)/2;
int y = (screenSize.height-h)/2;
Configuration config = new Configuration();
// 将刚才获得的值一一设置进Configuration类的config实例对象中
config.setMainWindowLocation(new Point(x, y));
config.setMainWindowSize(new Dimension(w, h));
config.setMainWindowMaximize(false);
// 好像是要从win电脑的系统环境中提取什么值,但是在程序测试中是null
String defaultLaf = System.getProperty("swing.defaultlaf");
config.setLookAndFeel((defaultLaf != null) ? defaultLaf : UIManager.getSystemLookAndFeelClassName());
// System.out.println(System.out.printf("user.dir"));
// 获取了文件夹,好像是拿到了程序所在的文件夹
// recentSaveDirectory对象中包含这个文件下所有的一级文件夹和文件
File recentSaveDirectory = new File(System.getProperty("user.dir"));
config.setRecentLoadDirectory(recentSaveDirectory);
config.setRecentSaveDirectory(recentSaveDirectory);
// 如果这个jd-gui.cfg配置文件是存在的
if (FILE.exists()) {
// 使用文件流对象打开jd-gui.cfg文件
try (FileInputStream fis = new FileInputStream(FILE)) {
// 因为jd-gui.cfg写入的格式为xml 所以使用一个xml对象来解析
// 但是我又一个疑惑了,到底是什么时候才存在jd-gui的配置文件
// 因为看下面的操作,在jd-gui.cfg中好像已经存在了key
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(fis);
// Load values
String name = "";
Stack<String> names = new Stack<>();
List<File> recentFiles = new ArrayList<>();
boolean maximize = false;
Map<String, String> preferences = config.getPreferences();
// 利用xml语法根据key value的形式获取配置文件中的值?不过比较疑惑的地方在于 这些内容是什么时候放进去的呢?(20210802)
while (reader.hasNext()) {
switch (reader.next()) {
case XMLStreamConstants.START_ELEMENT:
names.push(name);
name += '/' + reader.getLocalName();
switch (name) {
case "/configuration/gui/mainWindow/location":
x = Integer.parseInt(reader.getAttributeValue(null, "x"));
y = Integer.parseInt(reader.getAttributeValue(null, "y"));
break;
case "/configuration/gui/mainWindow/size":
w = Integer.parseInt(reader.getAttributeValue(null, "w"));
h = Integer.parseInt(reader.getAttributeValue(null, "h"));
break;
}
break;
case XMLStreamConstants.END_ELEMENT:
name = names.pop();
break;
case XMLStreamConstants.CHARACTERS:
switch (name) {
case "/configuration/recentFilePaths/filePath":
File file = new File(reader.getText().trim());
if (file.exists()) {
recentFiles.add(file);
}
break;
case "/configuration/recentDirectories/loadPath":
file = new File(reader.getText().trim());
if (file.exists()) {
config.setRecentLoadDirectory(file);
}
break;
case "/configuration/recentDirectories/savePath":
file = new File(reader.getText().trim());
if (file.exists()) {
config.setRecentSaveDirectory(file);
}
break;
case "/configuration/gui/lookAndFeel":
config.setLookAndFeel(reader.getText().trim());
break;
case "/configuration/gui/mainWindow/maximize":
maximize = Boolean.parseBoolean(reader.getText().trim());
break;
default:
if (name.startsWith("/configuration/preferences/")) {
String key = name.substring("/configuration/preferences/".length());
preferences.put(key, reader.getText().trim());
}
break;
}
break;
}
}
if (recentFiles.size() > Constants.MAX_RECENT_FILES) {
// Truncate
recentFiles = recentFiles.subList(0, Constants.MAX_RECENT_FILES);
}
config.setRecentFiles(recentFiles);
if ((x >= 0) && (y >= 0) && (x + w < screenSize.width) && (y + h < screenSize.height)) {
// Update preferences
config.setMainWindowLocation(new Point(x, y));
config.setMainWindowSize(new Dimension(w, h));
config.setMainWindowMaximize(maximize);
}
reader.close();
} catch (Exception e) {
assert ExceptionUtil.printStackTrace(e);
}
}
if (! config.getPreferences().containsKey(ERROR_BACKGROUND_COLOR)) {
config.getPreferences().put(ERROR_BACKGROUND_COLOR, "0xFF6666");
}
config.getPreferences().put(JD_CORE_VERSION, getJdCoreVersion());
return config;
}
jd-gui在对配置文件读取的方式为:xml。
在正式解析前,会判定配置文件是否存在。