package www.hub;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;
public class PropertyDemo {
public static void main(String[] args) {
Properties p = new Properties();
try {
//读取jar内部文件路径
p.load(PropertyDemo.class.getResourceAsStream("test.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Entry<Object, Object> kv : p.entrySet()) {
System.out.println(kv.getKey() + "-" + kv.getValue());
}
//读取jar外部文件下面输出的是工程目录所在的路径D/workspace/tt
System.out.println(System.getProperty("user.dir"));
String filePath = System.getProperty("user.dir") + "/test.properties";
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//下面输出的是相同的都是编译后的classes文件夹路径,也可以用来作为获取路径的方法
System.out.println(PropertyDemo.class.getClassLoader().getResource("").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
}
}
另外java中常用的获取路径的方法,https://www.cnblogs.com/franson-2016/p/5728280.html