📄 propertiesutil.java
字号:
package cn.js.fan.util;import java.io.*;import javax.servlet.http.*;import javax.servlet.*;import javax.servlet.jsp.*;import java.util.Set;public class PropertiesUtil { private String fileName; private Properties p; private FileInputStream in; private FileOutputStream out; String charset = "gb2312"; public PropertiesUtil(String fileName, String charset) { this.fileName = fileName; this.charset = charset; File file = new File(fileName); try { in = new FileInputStream(file); p = new Properties(charset); p.load(in, charset); in.close(); } catch (FileNotFoundException e) { System.err.println("配置文件config.properties找不到!"); e.printStackTrace(); } catch (Exception e) { System.err.println("读取配置文件config.properties错误!"); e.printStackTrace(); } } public PropertiesUtil(String fileName) { this.fileName = fileName; File file = new File(fileName); try { in = new FileInputStream(file); p = new Properties(charset); p.load(in); in.close(); } catch (FileNotFoundException e) { System.err.println("配置文件config.properties找不到!"); e.printStackTrace(); } catch (Exception e) { System.err.println("读取配置文件config.properties错误!"); e.printStackTrace(); } } public static String getConfigFile(HttpServlet hs) { return getConfigFile(hs, "config.properties"); } public static String getConfigFile(HttpServlet hs, String configFileName) { String configFile = ""; ServletContext sc = hs.getServletContext(); configFile = sc.getRealPath("/" + configFileName); if (configFile == null || configFile.equals("")) { configFile = "/" + configFileName; } return configFile; } public static String getConfigFile(PageContext hs, String configFileName) { String configFile = ""; ServletContext sc = hs.getServletContext(); configFile = sc.getRealPath("/" + configFileName); if (configFile == null || configFile.equals("")) { configFile = "/" + configFileName; } return configFile; } public Set getKeys() { return p.keySet(); } public String getValue(String itemName) { String str = ""; try { str = new String(p.getProperty(itemName).getBytes("ISO8859_1"), charset); } catch (Exception e) { System.out.println("PropertiesUtil: getValue " + e.getMessage()); } return str; } public String getValue(String itemName, String defaultValue) { return p.getProperty(itemName, defaultValue); } public void setValue(String itemName, String value) { try { value = new String(value.getBytes(charset), "ISO8859_1"); } catch (Exception e) { System.out.println("PropertiesUtil: setValue " + e.getMessage()); } p.setProperty(itemName, value); return; } public void saveFile(String fileName, String description) throws Exception { try { File f = new File(fileName); out = new FileOutputStream(f); p.store(out, description); out.close(); } catch (IOException ex) { throw new Exception ("无法保存指定的配置文件:" + fileName); } } public void saveFile(String fileName) throws Exception { saveFile(fileName, ""); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -