最近写代码遇到一个获取项目跟路径,看到一个前辈这样写道:
String path = System.getProperty("user.dir");
然后,网上百度了一下, System.getProperty(“key”)可以获取很东西。
public static void getSystemProperties(){
File fileName = new File("E:/getdemo","pors.properties");
Properties pros = System.getProperties();
try {
pros.store(new FileOutputStream(fileName), "Properties ClASS");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
getSystemProperties();
}
在运行之前先要,在E盘创建文件夹getdemo,不然会报错
打开生成的后缀为.properties文件后你会看到很多,其中包括上面提到的user.dir:
去properties中去寻找你的所爱吧,不要忘记点赞哦@@@@@