1. 读取jar包外面的文件
在同一个文件夹(比如target)有conf文件夹,和test-0.1.jar文件。
prop1.properties 放在conf里面,test-0.1.jar读取prop1.properties 的pro1.value的值
- 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”));
- }
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包里面根目录下面
- public static void fun2() throws IOException{
- // path 不以’/’开头时默认是从此类所在的包下取资源,以’/’开头则是从
- InputStream in = new Dog().getClass().getResourceAsStream(“/prop2.properties”);
- operties p = new Properties();
- p.load(in);
- // I am 2, inside jar
- System.out.println(p.getProperty(”pro2.value”));
- }
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的值
- 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”));
- }
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包里面根目录下面
- public static void fun2() throws IOException{
- // path 不以’/’开头时默认是从此类所在的包下取资源,以’/’开头则是从
- InputStream in = new Dog().getClass().getResourceAsStream(“/prop2.properties”);
- operties p = new Properties();
- p.load(in);
- // I am 2, inside jar
- System.out.println(p.getProperty(”pro2.value”));
- }
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>