import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.apache.log4j.Logger; public class SystemParameter { private static Logger logger = Logger.getLogger(SystemParameter.class); private static String msgTemplate1; private static String msgTemplate2; private static String msgTemplate3; private static final Properties properties; public static Map<String, String> proMap=new HashMap<String, String>(); private SystemParameter() { } static { properties = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream(Thread.currentThread() .getContextClassLoader().getResource( "systemParamter.properties").toString() .substring(6)); properties.load(fis); msgTemplate1 = properties.getProperty("msgTemplate1"); proMap.put("msgTemplate1", msgTemplate1); msgTemplate2 = properties.getProperty("msgTemplate2"); proMap.put("msgTemplate2", msgTemplate2); msgTemplate3 = properties.getProperty("msgTemplate3"); proMap.put("msgTemplate3", msgTemplate3); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getMsgTemplate1() { return proMap.get("msgTemplate1"); } public static String getMsgTemplate2() { return proMap.get("msgTemplate2"); } public static String getMsgTemplate3() { return proMap.get("msgTemplate3"); } // 修改通知模板 public static void modifyMsgTemplate(String key,String template) { logger.debug("################" + template); properties.setProperty(key, template); FileOutputStream fos = null; try { fos = new FileOutputStream(Thread.currentThread() .getContextClassLoader().getResource( "systemParamter.properties").toString() .substring(6)); properties.store(fos, ""); proMap.put(key, template); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 另一个例子 public class Test { private static Properties properties = new Properties(); public static void main(String[] args) { try { InputStream is = Test. class .getClassLoader().getResourceAsStream( "cache.properties" ); properties.load(is); String size = properties.getProperty( "cache.size" ); writeLog( "配置成功!" + size); } catch (FileNotFoundException e) { writeLog( "配置文件不存在!" + e.getMessage()); } catch (IOException e) { writeLog( "读取配置文件IO错误!" + e.getMessage()); } } public static void writeLog(String strLog) { System.out.println(strLog); } } 1.使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); return rb.getString("key"); 注:该方法可以读jar包里的文件 2.使用java.util.PropertyResourceBundle类的构造函数 示例: InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in); return rb.getString("key"); 3.使用java.util.Properties类的load()方法 示例: InputStream in = new BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); return p.getProperties("key"); or return p.getProperties("key", "defaultValue"); 4.使用class变量的getResourceAsStream()方法 示例: InputStream in = ClassName.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in); return p.getProperties("key"); or return p.getProperties("key", "defaultValue"); 5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例: InputStream in = ClassName.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in); return p.getProperties("key"); or return p.getProperties("key", "defaultValue"); 6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 示例: InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in); return p.getProperties("key"); or return p.getProperties("key", "defaultValue"); 补充 Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:InputStream in = context.getResourceAsStream(path); Properties p = new Properties(); p.load(in); return p.getProperties("key"); or return p.getProperties("key", "defaultValue"); 7.读jar外面的文件 FileInputStream fis; Properties p = new Properties(); fis = new FileInputStream(initfileName); prop.load(fis); 注:jar文件执行时,注意cmd路径必须与jar路径相同