📄 configfileparser.java
字号:
package net.sujee.util;import java.io.IOException;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Properties;public class ConfigFileParser{ File mConfigFile; Properties prop; public ConfigFileParser (String fileName) throws IOException { prop = new Properties (); mConfigFile = new File (getFileLocation(fileName)); if ( mConfigFile.exists() && mConfigFile.isFile() && mConfigFile.canRead() ) { parseFile (); return; } // last thing throw exeception throw new IOException ("ConfigFileParser : file '" + fileName + "' doesn't exist"); } public String getFileLocation (String name) { // try something plain File file = new File (name); if ( file.exists() && file.isFile() && file.canRead() ) return file.getAbsolutePath(); // now search current dir. String pwd = (String) System.getProperty("user.dir"); String slash = (String) System.getProperty("file.separator"); file = new File (pwd + slash + name); if ( file.exists() && file.isFile() && file.canRead() ) return file.getAbsolutePath(); // now search home dir. String homeDir = (String) System.getProperty("user.home"); file = new File (homeDir + slash + name); if ( file.exists() && file.isFile() && file.canRead() ) return file.getAbsolutePath(); // if not return NULL return null; } public String getConfigProperty (String name, boolean trim) { String s = (String) prop.get(name); if ( (trim) && s != null) return s.trim(); else return s; } public int readIntProperty (String propName, int defaultValue) { try { int i = Integer.parseInt(getConfigProperty(propName, true)); return i; } catch (Exception e) { return defaultValue; } } public void savePropertiesToFile () { try { FileOutputStream fout = new FileOutputStream (mConfigFile); prop.store( fout,"Config file for project timer" ); fout.close(); } catch (IOException e) { e.printStackTrace(); } } public void setProperty (Object key, Object value) { prop.put (key, value); } private void parseFile () throws IOException { prop.load(new FileInputStream (mConfigFile)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -