溫馨提示×

java如何獲取當前項目的路徑地址

小億
171
2023-09-13 17:36:31
欄目: 編程語言

在Java中,可以使用以下代碼獲取當前項目的路徑地址:

  1. 使用System.getProperty("user.dir")方法獲取當前項目的工作目錄路徑,代碼如下:
String projectPath = System.getProperty("user.dir");
System.out.println("當前項目路徑:" + projectPath);
  1. 使用ClassLoader類的getResource()方法獲取當前類的絕對路徑,然后通過截取可以得到當前項目的路徑,代碼如下:
String classPath = getClass().getResource("").getPath();
String projectPath = classPath.substring(0, classPath.indexOf("target"));
System.out.println("當前項目路徑:" + projectPath);
  1. 使用Paths.get("").toAbsolutePath().toString()方法獲取當前項目的絕對路徑,代碼如下:
String projectPath = Paths.get("").toAbsolutePath().toString();
System.out.println("當前項目路徑:" + projectPath);

以上方法都可以用來獲取當前項目的路徑地址,具體使用哪種方法取決于你的需求和項目結構。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女