org.apache.log4j.helpers.Loader加载资源文件

来源:互联网 发布:淘宝衣服拍照价格 编辑:程序博客网 时间:2024/06/05 19:57

**org.apache.log4j.helpers.Loader加载资源文件**

在几个maven项目中用 org.apache.log4j.helpers.Loader.getResource("test.properties")
引入另一个项目的配置文件.

部分代码如下:

static {  readPropertiesFile();}   public void readProperties(String fileName){    Properties prop= new Properties();    try {        InputStream in = new BufferedInputStream(new FileInputStream(fileName));        prop.load(in);        String name = prop.getProperty("username");        String maxSize = prop.getProperty("maxSize");        //......        in.close();    } catch (Exception e) {        //...    }}protected static void readPropertiesFile(){    URL url = Loader.getResource("remoteResource.properties");    try {      if (url != null){        String systemEncoding = new OutputStreamWriter(new   ByteArrayOutputStream()).getEncoding();        String urlpath = "";        if ((systemEncoding == null) || (systemEncoding.trim().length() < 1)) {               urlpath = URLDecoder.decode(url.getPath(), "UTF-8");      } else {               urlpath = URLDecoder.decode(url.getPath(), systemEncoding);      }          String fileRealFileSystemPath = new File(urlpath).getAbsolutePath();        readProperties(fileRealFileSystemPath);      } else{             throw new Exception("remoteResource.properties");      }    }catch (UnsupportedEncodingException e) {    }catch (Exception e){    }}

将方法放入静态块,在类初始化的时候旧读取所需文件。

原创粉丝点击