zoukankan      html  css  js  c++  java
  • Java读取properties文件

    1.jsp页面展示代码


    2. 代码结构图


    其中common中的Prop.java是读取properties文件

    3.代码实现

    一.初版

    通过上述方法能够实现读出properties文件中的内容。但是存在两个缺陷
    1. class.gerResourceAsStream()方法默认路径是当前类路径。对于不同的properties文件必须放在此类目录下/或者写完整路径。
    2. inputStream没有定义编码方式,如果properties中的文字是中文的。则会出现乱码
    3. inputStream 异常不应当抛出,而应该在此类中捕获。

    二. 修正
    系统加载器,类加载器,当前线程的上下文加载器。通过线程的上下文加载器

    Thread.currentThread.getContextClassLoader()来查找这个文件
    代码如下:

    三. 增加缓存
    文件读取涉及IO流的操作。增加缓存机制
    新增PropUtil.java
  • 相关阅读:
    Interesting Finds: 2008.03.19
    Interesting Finds: 2008.03.11
    Interesting Finds: 2008.03.27
    Interesting Finds: 2008.03.21
    每日日报
    每日日报
    每日日报
    每日日报
    idea怎么创建properties文件
    移动端rem.js
  • 原文地址:https://www.cnblogs.com/stoneFang/p/6715314.html
Copyright © 2011-2022 走看看