Maven打包jar后文件读取资源文件

1. 读取jar包外面的文件

在同一个文件夹(比如target)有conf文件夹,和test-0.1.jar文件。

prop1.properties 放在conf里面,test-0.1.jar读取prop1.properties 的pro1.value的值

  1.   public static void fun1() throws IOException{  
  2.     String str1 = new Dog().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();  
  3.     //   /H:/Users/OTC/workspace/testLog4j/target/testLog4j-0.1.jar  
  4.     System.out.println(str1);  
  5.     String str2 = new File(str1).getParent() + “/conf/prop1.properties”;  
  6.     //   H:\Users\OTC\workspace\testLog4j\target/conf/prop1.properties  
  7.     System.out.println(str2);  
  8. InputStream in = new BufferedInputStream(new FileInputStream(str2));  
  9. Properties p = new Properties();  
  10.       p.load(in);  
  11.       //    I am 1, outside jar  
  12.       System.out.println(p.getProperty(”pro1.value”));  
  13.   }  
    public static void fun1() throws IOException{
        String str1 = new Dog().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        //   /H:/Users/OTC/workspace/testLog4j/target/testLog4j-0.1.jar
        System.out.println(str1);
        String str2 = new File(str1).getParent() + "/conf/prop1.properties";
        //   H:\Users\OTC\workspace\testLog4j\target/conf/prop1.properties
        System.out.println(str2);
        InputStream in = new BufferedInputStream(new FileInputStream(str2));
        Properties p = new Properties();
        p.load(in);
        //    I am 1, outside jar
        System.out.println(p.getProperty("pro1.value"));
    }



2. 读取jar包里面的文件

prop2.properties放在jar包里面根目录下面

  1. public static void fun2() throws IOException{  
  2.     //  path 不以’/’开头时默认是从此类所在的包下取资源,以’/’开头则是从  
  3.     InputStream in = new Dog().getClass().getResourceAsStream(“/prop2.properties”);  
  4. operties p = new Properties();  
  5.     p.load(in);  
  6.     //   I am 2, inside jar  
  7.     System.out.println(p.getProperty(”pro2.value”));  
  8. }  
    public static void fun2() throws IOException{
        //  path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从
        InputStream in = new Dog().getClass().getResourceAsStream("/prop2.properties");
        Properties p = new Properties();
        p.load(in);
        //   I am 2, inside jar
        System.out.println(p.getProperty("pro2.value"));
    }




                </div>

1. 读取jar包外面的文件

在同一个文件夹(比如target)有conf文件夹,和test-0.1.jar文件。

prop1.properties 放在conf里面,test-0.1.jar读取prop1.properties 的pro1.value的值

  1.   public static void fun1() throws IOException{  
  2.     String str1 = new Dog().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();  
  3.     //   /H:/Users/OTC/workspace/testLog4j/target/testLog4j-0.1.jar  
  4.     System.out.println(str1);  
  5.     String str2 = new File(str1).getParent() + “/conf/prop1.properties”;  
  6.     //   H:\Users\OTC\workspace\testLog4j\target/conf/prop1.properties  
  7.     System.out.println(str2);  
  8. InputStream in = new BufferedInputStream(new FileInputStream(str2));  
  9. Properties p = new Properties();  
  10.       p.load(in);  
  11.       //    I am 1, outside jar  
  12.       System.out.println(p.getProperty(”pro1.value”));  
  13.   }  
    public static void fun1() throws IOException{
        String str1 = new Dog().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        //   /H:/Users/OTC/workspace/testLog4j/target/testLog4j-0.1.jar
        System.out.println(str1);
        String str2 = new File(str1).getParent() + "/conf/prop1.properties";
        //   H:\Users\OTC\workspace\testLog4j\target/conf/prop1.properties
        System.out.println(str2);
        InputStream in = new BufferedInputStream(new FileInputStream(str2));
        Properties p = new Properties();
        p.load(in);
        //    I am 1, outside jar
        System.out.println(p.getProperty("pro1.value"));
    }



2. 读取jar包里面的文件

prop2.properties放在jar包里面根目录下面

  1. public static void fun2() throws IOException{  
  2.     //  path 不以’/’开头时默认是从此类所在的包下取资源,以’/’开头则是从  
  3.     InputStream in = new Dog().getClass().getResourceAsStream(“/prop2.properties”);  
  4. operties p = new Properties();  
  5.     p.load(in);  
  6.     //   I am 2, inside jar  
  7.     System.out.println(p.getProperty(”pro2.value”));  
  8. }  
    public static void fun2() throws IOException{
        //  path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从
        InputStream in = new Dog().getClass().getResourceAsStream("/prop2.properties");
        Properties p = new Properties();
        p.load(in);
        //   I am 2, inside jar
        System.out.println(p.getProperty("pro2.value"));
    }




                </div>

猜你喜欢

转载自blog.csdn.net/qq_21683643/article/details/79854897