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

本文介绍如何使用org.apache.log4j.helpers.Loader加载远程资源文件,并解析properties文件内容。通过静态初始化块实现类加载时自动读取配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**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){

    }
}

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

补充说明 fa@zcn:~/mycode/simulation$ rufa@zcn:~/mycode/simulation$ dpkg -L liblog4cxx-dev # 确认包含log4cxx-config.cmake文件 /. /usr /usr/include /usr/include/log4cxx /usr/include/log4cxx/appender.h /usr/include/log4cxx/appenderskeleton.h /usr/include/log4cxx/asyncappender.h /usr/include/log4cxx/basicconfigurator.h /usr/include/log4cxx/config /usr/include/log4cxx/config/propertysetter.h /usr/include/log4cxx/consoleappender.h /usr/include/log4cxx/dailyrollingfileappender.h /usr/include/log4cxx/db /usr/include/log4cxx/db/odbcappender.h /usr/include/log4cxx/defaultconfigurator.h /usr/include/log4cxx/defaultloggerfactory.h /usr/include/log4cxx/file.h /usr/include/log4cxx/fileappender.h /usr/include/log4cxx/filter /usr/include/log4cxx/filter/andfilter.h /usr/include/log4cxx/filter/denyallfilter.h /usr/include/log4cxx/filter/expressionfilter.h /usr/include/log4cxx/filter/levelmatchfilter.h /usr/include/log4cxx/filter/levelrangefilter.h /usr/include/log4cxx/filter/locationinfofilter.h /usr/include/log4cxx/filter/mapfilter.h /usr/include/log4cxx/filter/propertyfilter.h /usr/include/log4cxx/filter/stringmatchfilter.h /usr/include/log4cxx/helpers /usr/include/log4cxx/helpers/absolutetimedateformat.h /usr/include/log4cxx/helpers/appenderattachableimpl.h /usr/include/log4cxx/helpers/aprinitializer.h /usr/include/log4cxx/helpers/bufferedoutputstream.h /usr/include/log4cxx/helpers/bufferedwriter.h /usr/include/log4cxx/helpers/bytearrayinputstream.h /usr/include/log4cxx/helpers/bytearrayoutputstream.h /usr/include/log4cxx/helpers/bytebuffer.h /usr/include/log4cxx/helpers/cacheddateformat.h /usr/include/log4cxx/helpers/charsetdecoder.h /usr/include/log4cxx/helpers/charsetencoder.h /usr/include/log4cxx/helpers/class.h /usr/include/log4cxx/helpers/classregistration.h /usr/include/log4cxx/helpers/condition.h /usr/include/log4cxx/helpers/cyclicbuffer.h /usr/include/log4cxx/helpers/datagrampacket.h /usr/include/log4cxx/helpers/datagramsocket.h /usr/include/log4cxx/helpers/date.h /usr/include/log4cxx/helpers/dateformat.h /usr/include/log4cxx/helpers/datelayout.h /usr/include/log4cxx/helpers/datetimedateformat.h /usr/include/log4cxx/helpers/exception.h /usr/include/log4cxx/helpers/fileinputstream.h /usr/include/log4cxx/helpers/fileoutputstream.h /usr/include/log4cxx/helpers/filewatchdog.h /usr/include/log4cxx/helpers/inetaddress.h /usr/include/log4cxx/helpers/inputstream.h /usr/include/log4cxx/helpers/inputstreamreader.h /usr/include/log4cxx/helpers/integer.h /usr/include/log4cxx/helpers/iso8601dateformat.h /usr/include/log4cxx/helpers/loader.h /usr/include/log4cxx/helpers/locale.h /usr/include/log4cxx/helpers/loglog.h /usr/include/log4cxx/helpers/messagebuffer.h /usr/include/log4cxx/helpers/mutex.h /usr/include/log4cxx/helpers/object.h /usr/include/log4cxx/helpers/objectimpl.h /usr/include/log4cxx/helpers/objectoutputstream.h /usr/include/log4cxx/helpers/objectptr.h /usr/include/log4cxx/helpers/onlyonceerrorhandler.h /usr/include/log4cxx/helpers/optionconverter.h /usr/include/log4cxx/helpers/outputstream.h /usr/include/log4cxx/helpers/outputstreamwriter.h /usr/include/log4cxx/helpers/pool.h /usr/include/log4cxx/helpers/properties.h /usr/include/log4cxx/helpers/propertyresourcebundle.h /usr/include/log4cxx/helpers/reader.h /usr/include/log4cxx/helpers/relativetimedateformat.h /usr/include/log4cxx/helpers/resourcebundle.h /usr/include/log4cxx/helpers/serversocket.h /usr/include/log4cxx/helpers/simpledateformat.h /usr/include/log4cxx/helpers/socket.h /usr/include/log4cxx/helpers/socketoutputstream.h /usr/include/log4cxx/helpers/strftimedateformat.h /usr/include/log4cxx/helpers/strictmath.h /usr/include/log4cxx/helpers/stringhelper.h /usr/include/log4cxx/helpers/stringtokenizer.h /usr/include/log4cxx/helpers/synchronized.h /usr/include/log4cxx/helpers/syslogwriter.h /usr/include/log4cxx/helpers/system.h /usr/include/log4cxx/helpers/systemerrwriter.h /usr/include/log4cxx/helpers/systemoutwriter.h /usr/include/log4cxx/helpers/tchar.h /usr/include/log4cxx/helpers/thread.h /usr/include/log4cxx/helpers/threadlocal.h /usr/include/log4cxx/helpers/threadspecificdata.h /usr/include/log4cxx/helpers/timezone.h /usr/include/log4cxx/helpers/transcoder.h /usr/include/log4cxx/helpers/transform.h /usr/include/log4cxx/helpers/writer.h /usr/include/log4cxx/helpers/xml.h /usr/include/log4cxx/hierarchy.h /usr/include/log4cxx/htmllayout.h /usr/include/log4cxx/layout.h /usr/include/log4cxx/level.h /usr/include/log4cxx/log4cxx.h /usr/include/log4cxx/logger.h /usr/include/log4cxx/logmanager.h /usr/include/log4cxx/logstring.h /usr/include/log4cxx/mdc.h /usr/include/log4cxx/ndc.h /usr/include/log4cxx/net /usr/include/log4cxx/net/smtpappender.h /usr/include/log4cxx/net/socketappender.h /usr/include/log4cxx/net/socketappenderskeleton.h /usr/include/log4cxx/net/sockethubappender.h /usr/include/log4cxx/net/syslogappender.h /usr/include/log4cxx/net/telnetappender.h /usr/include/log4cxx/net/xmlsocketappender.h /usr/include/log4cxx/nt /usr/include/log4cxx/nt/nteventlogappender.h /usr/include/log4cxx/nt/outputdebugstringappender.h /usr/include/log4cxx/pattern /usr/include/log4cxx/pattern/classnamepatternconverter.h /usr/include/log4cxx/pattern/datepatternconverter.h /usr/include/log4cxx/pattern/filedatepatternconverter.h /usr/include/log4cxx/pattern/filelocationpatternconverter.h /usr/include/log4cxx/pattern/formattinginfo.h /usr/include/log4cxx/pattern/fulllocationpatternconverter.h /usr/include/log4cxx/pattern/integerpatternconverter.h /usr/include/log4cxx/pattern/levelpatternconverter.h /usr/include/log4cxx/pattern/linelocationpatternconverter.h /usr/include/log4cxx/pattern/lineseparatorpatternconverter.h /usr/include/log4cxx/pattern/literalpatternconverter.h /usr/include/log4cxx/pattern/loggerpatternconverter.h /usr/include/log4cxx/pattern/loggingeventpatternconverter.h /usr/include/log4cxx/pattern/messagepatternconverter.h /usr/include/log4cxx/pattern/methodlocationpatternconverter.h /usr/include/log4cxx/pattern/nameabbreviator.h /usr/include/log4cxx/pattern/namepatternconverter.h /usr/include/log4cxx/pattern/ndcpatternconverter.h /usr/include/log4cxx/pattern/patternconverter.h /usr/include/log4cxx/pattern/patternparser.h /usr/include/log4cxx/pattern/propertiespatternconverter.h /usr/include/log4cxx/pattern/relativetimepatternconverter.h /usr/include/log4cxx/pattern/threadpatternconverter.h /usr/include/log4cxx/pattern/throwableinformationpatternconverter.h /usr/include/log4cxx/patternlayout.h /usr/include/log4cxx/portability.h /usr/include/log4cxx/private /usr/include/log4cxx/private/log4cxx_private.h /usr/include/log4cxx/propertyconfigurator.h /usr/include/log4cxx/provisionnode.h /usr/include/log4cxx/rolling /usr/include/log4cxx/rolling/action.h /usr/include/log4cxx/rolling/filerenameaction.h /usr/include/log4cxx/rolling/filterbasedtriggeringpolicy.h /usr/include/log4cxx/rolling/fixedwindowrollingpolicy.h /usr/include/log4cxx/rolling/gzcompressaction.h /usr/include/log4cxx/rolling/manualtriggeringpolicy.h /usr/include/log4cxx/rolling/rollingfileappender.h /usr/include/log4cxx/rolling/rollingfileappenderskeleton.h /usr/include/log4cxx/rolling/rollingpolicy.h /usr/include/log4cxx/rolling/rollingpolicybase.h /usr/include/log4cxx/rolling/rolloverdescription.h /usr/include/log4cxx/rolling/sizebasedtriggeringpolicy.h /usr/include/log4cxx/rolling/timebasedrollingpolicy.h /usr/include/log4cxx/rolling/triggeringpolicy.h /usr/include/log4cxx/rolling/zipcompressaction.h /usr/include/log4cxx/rollingfileappender.h /usr/include/log4cxx/simplelayout.h /usr/include/log4cxx/spi /usr/include/log4cxx/spi/appenderattachable.h /usr/include/log4cxx/spi/configurator.h /usr/include/log4cxx/spi/defaultrepositoryselector.h /usr/include/log4cxx/spi/errorhandler.h /usr/include/log4cxx/spi/filter.h /usr/include/log4cxx/spi/hierarchyeventlistener.h /usr/include/log4cxx/spi/location /usr/include/log4cxx/spi/location/locationinfo.h /usr/include/log4cxx/spi/loggerfactory.h /usr/include/log4cxx/spi/loggerrepository.h /usr/include/log4cxx/spi/loggingevent.h /usr/include/log4cxx/spi/optionhandler.h /usr/include/log4cxx/spi/repositoryselector.h /usr/include/log4cxx/spi/rootlogger.h /usr/include/log4cxx/spi/triggeringeventevaluator.h /usr/include/log4cxx/stream.h /usr/include/log4cxx/ttcclayout.h /usr/include/log4cxx/varia /usr/include/log4cxx/varia/fallbackerrorhandler.h /usr/include/log4cxx/writerappender.h /usr/include/log4cxx/xml /usr/include/log4cxx/xml/domconfigurator.h /usr/include/log4cxx/xml/xmllayout.h /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/liblog4cxx.a /usr/lib/x86_64-linux-gnu/pkgconfig /usr/lib/x86_64-linux-gnu/pkgconfig/liblog4cxx.pc /usr/share /usr/share/doc /usr/share/doc/liblog4cxx-dev /usr/share/doc/liblog4cxx-dev/copyright /usr/share/doc/liblog4cxx-dev/examples /usr/share/doc/liblog4cxx-dev/examples/Makefile.am /usr/share/doc/liblog4cxx-dev/examples/console.cpp /usr/share/doc/liblog4cxx-dev/examples/delayedloop.cpp /usr/share/doc/liblog4cxx-dev/examples/stream.cpp /usr/share/doc/liblog4cxx-dev/examples/trivial.cpp /usr/share/lintian /usr/share/lintian/overrides /usr/share/lintian/overrides/liblog4cxx-dev /usr/lib/x86_64-linux-gnu/liblog4cxx.so /usr/share/doc/liblog4cxx-dev/NOTICE /usr/share/doc/liblog4cxx-dev/changelog.Debian.gz rufa@zcn:~/mycode/simulation$
最新发布
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值