zoukankan      html  css  js  c++  java
  • Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:

           InputStream in = this.getClass().getResourceAsStream("资源Name");

        注意:

        (1)这种方式要求 Properties资源文件必须与当前类文件在同一个包下(同文件夹下) ,如果不在则会报空指针异常,如果不在同一个包(文件夹)下可以使用,如果方     

             式:

            InputStream in = this.getClass().getResourceAsStream("/testcase/test.txt"); 

            或

           InputStream in = PropertiesUtil.class.getResourceAsStream("/testcase/test.txt"); 

        (2:

            String packagePath = this.getClass().getResource("").getPath();

    2.使用Class.getClassLoader()的getResourceAsStream()读取Properties文件(资源文件)的路径问题:

          InputStream in = this.getClass().getClassLoader().getResourceAsStream("testcase/test.txt");

       注意:

       (1)使用getClassLoader()获取的是classpath路径;

      

       (2)虽然也可以使用Object.class.getClassLoader().getResourceAsStream("/testcase/test.txt")来获取资源文件,但是如果在Web项目中的话,会得到一个Null值,所以

            保 险 起见,就是这个类的本身名字来直接获取Class对象,如果我这个类为PropertiesUtil.java,可以使用如下方式获取:

             PropertiesUtil.class.getClassLoader().getResourceAsStream("/testcase/test.txt");

    https://www.cnblogs.com/tk55/p/6064735.html

  • 相关阅读:
    Window 窗口类
    使用 Bolt 实现 GridView 表格控件
    lua的table库
    Windows编程总结之 DLL
    lua 打印 table 拷贝table
    使用 xlue 实现简单 listbox 控件
    使用 xlue 实现 tips
    extern “C”
    COleVariant如何转换为int double string cstring
    原来WIN32 API也有GetOpenFileName函数
  • 原文地址:https://www.cnblogs.com/twoheads/p/9834933.html
Copyright © 2011-2022 走看看