1. 项目根路径获取
(1)new File("").getAbsolutePath()
输出:=getAbsolutePath====D:\project\SVN\2-系统开发实施\(3)编码\personnel-positioning-data-collector
(2) System.getProperty("user.dir")
输出:====user.dir===D:\project\SVN\2-系统开发实施\(3)编码\personnel-positioning-data-collector
(3) new File("").getCanonicalPath();
输出:======file.getFCanonicalPath=====D:\project\SVN\2-系统开发实施\(3)编码\personnel-positioning-data-collector
2.类资源加载路径获取
System.out.println("===类资源根路径getResource(/)====" : GeoShapServiceImpl.class.getResource("/").getPath());
输出:===getResource(/)====/D:/project/SVN/2-%e7%b3%bb%e7%bb%9f%e5%bc%80%e5%8f%91%e5%ae%9e%e6%96%bd/(3)%e7%bc%96%e7%a0%81/personnel-positioning-data-collector/positioning-data-geomapfile/target/classes/
System.out.println("====当前类资源路径getResource()===" + GeoShapServiceImpl.class.getResource("").getPath());
输出:====getResource()===/D:/project/SVN/2-%e7%b3%bb%e7%bb%9f%e5%bc%80%e5%8f%91%e5%ae%9e%e6%96%bd/(3)%e7%bc%96%e7%a0%81/personnel-positioning-data-collector/positioning-data-geomapfile/target/classes/geomapfile/service/impl/
3.获取类库资源加载路径
System.out.println("==========java.class.path======" + System.getProperty("java.class.path"));
输出(截取部分):==========java.class.path======C:\Program Files\Java\jdk1.8.0_241\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;C:\Program Fpositioning-data-collector\positioning-data-geomapfile\target\classes;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot-starter\2.0.4.RELEASE\spring-boot-starter-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot\2.0.4.RELEASE\spring-boot-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\spring-context\5.0.8.RELEASE\spring-context-5.0.8.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot-autoconfigure\2.0.4.RELEASE\spring-boot-autoconfigure-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\org\springframework\boot\spring-boot-starter-logging\2.0.4.RELEASE\spring-boot-starter-logging-2.0.4.RELEASE.jar;D:\Users\zhanglu\local\apache-maven-3.6.1\my-maven\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar
4.解决Properties中文乱码问题
其中需要把properties放到resources中,与application.yml在同级路径,结构如下图所示:
注:强调下文件路径存放问题主要是解决InputStream 对象为null的情况
try {
// InputStream in = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource("shapfile.properties").getFile()));
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("shapfile.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in, "gbk"));
Properties prop = new Properties();
String value = "";
prop.load(bf);
value = prop.getProperty("CQ");
System.out.println(value);
} catch (IOException e) {
e.printStackTrace();
}