程序中获得配置文件输入流和输出流的通用方法

来源:互联网 发布:百度自然搜索排名优化 编辑:程序博客网 时间:2024/06/05 10:27

 

class ServiceConfig{

private static Properties params = new Properties();

public static void load() {

 


  InputStream is = ServiceConfig.class.getResourceAsStream(configFile);
  try {
   params.load(is);
  } catch (Exception e) {
   throw new RuntimeException("无法读取配置信息:" + e.getMessage());
  }
 }

 public static void save() {
  URL url = ServiceConfig.class.getResource(configFile);
  try {
   OutputStream os = new FileOutputStream(url.getFile());
   params.store(os, null);
  } catch (Exception e) {
   throw new RuntimeException("无法将配置信息写入配置文件:" + e.getMessage());
  }
   }
  }

原创粉丝点击