多種情況下jar包獲取文件的路徑,讀取文件

代碼

代碼中,分別使用

  • AppMain.class.getResource
  • AppMain.class.getClassLoader().getResource
  • new File
  • System.getProperty
    來獲取路徑

其中resources文件夾還有一個a.json文件,模擬我們需要讀取的資源

package com.zgd.demo.test.filepath;

import java.io.File;
import java.net.URL;
import java.nio.file.Path;

/**
 * AppMain
 *
 * @author zgd
 * @date 2020/2/18 15:03
 */
public class AppMain {


  public static void main(String[] args) {
    //獲取當前文件所在的路徑
    URL localPath = AppMain.class.getResource("");
    System.out.println("AppMain.class.getResource(\"\").getPath() = " + (localPath == null ? "NullPointerException" : localPath.getPath()));

    //獲取當前文件所在的路徑
    URL localPath2 = AppMain.class.getResource("a.json");
    System.out.println("AppMain.class.getResource(\"a.json\").getPath() = "  + (localPath2 == null ? "NullPointerException" : localPath2.getPath()));

    //獲取項目根目錄
    URL rootPath = AppMain.class.getResource("/");
    System.out.println("AppMain.class.getResource(\"/\").getPath() = "  + (rootPath == null ? "NullPointerException" : rootPath.getPath()));

    URL rootPath2 = AppMain.class.getResource("/a.json");
    System.out.println("AppMain.class.getResource(\"/a.json\").getPath() = "  + (rootPath2 == null ? "NullPointerException" : rootPath2.getPath()));


    URL cl = AppMain.class.getClassLoader().getResource("");

    URL cl2 = AppMain.class.getClassLoader().getResource("a.json");

    URL cl3 = AppMain.class.getClassLoader().getResource("/");

    System.out.println("AppMain.class.getClassLoader().getResource(\"\").getPath() = " + (cl == null ? "NullPointerException" : cl.getPath()));
    System.out.println("AppMain.class.getClassLoader().getResource(\"a.json\").getPath() = " + (cl2 == null ? "NullPointerException" : cl2.getPath()));
    System.out.println("AppMain.class.getClassLoader().getResource(\"/\").getPath() = " + (cl3 == null ? "NullPointerException" : cl3.getPath()));



    String f = new File("").getPath();
    System.out.println("new File(\"\").getPath() = " + f);

    //當前工程的絕對路徑
    String absolutePath1 = new File("").getAbsolutePath();
    System.out.println("new File(\"\").getAbsolutePath() = " + absolutePath1);

    String absolutePath2 = new File("a.json").getAbsolutePath();
    System.out.println("new File(\"a.json\").getAbsolutePath() = " + absolutePath2);


    String absolutePath3 = new File("/").getAbsolutePath();
    System.out.println("new File(\"/\").getAbsolutePath() = " + absolutePath3);

    String absolutePath4 = new File("/a.json").getAbsolutePath();
    System.out.println("new File(\"/a.json\").getAbsolutePath() = " + absolutePath4);

    //獲取工程目錄
    File abc = new File("a.json");
    System.out.println("new File(\"a.json\").getPath() = " + abc.getPath());

    //獲取絕對路徑
    String absolutePath = abc.getAbsolutePath();
    System.out.println("new File(\"a.json\").getAbsolutePath() = " + absolutePath);


    //獲取相對路徑
    String path = abc.getPath();
    System.out.println("new File(\"a.json\").getPath() = " + path);

    System.out.println("new File(\"a.json\").exists() = " + abc.exists());

    // 獲取工程路徑
    String sp1 = System.getProperty("user.dir");
    System.out.println("System.getProperty(\"user.dir\") = " + sp1);
  }
  
}

一、idea運行情況

我的項目idea運行在: F:/work/test-filepath
我的包名是com.zgd.demo.test.filepath

AppMain.class.getResource("").getPath() = /F:/work/test-filepath/target/classes/com/zgd/demo/test/filepath/
AppMain.class.getResource(“a.json”).getPath() = NullPointerException
AppMain.class.getResource("/").getPath() = /F:/work/test-filepath/target/classes/
AppMain.class.getResource("/a.json").getPath() = /F:/work/test-filepath/target/classes/a.json
AppMain.class.getClassLoader().getResource("").getPath() = /F:/work/test-filepath/target/classes/
AppMain.class.getClassLoader().getResource(“a.json”).getPath() = /F:/work/test-filepath/target/classes/a.json
AppMain.class.getClassLoader().getResource("/").getPath() = NullPointerException
new File("").getPath() =
new File("").getAbsolutePath() = F:\work\test-filepath
new File(“a.json”).getAbsolutePath() = F:\work\test-filepath\a.json
new File("/").getAbsolutePath() = F:
new File("/a.json").getAbsolutePath() = F:\a.json
new File(“a.json”).getPath() = a.json
new File(“a.json”).getAbsolutePath() = F:\work\test-filepath\a.json
new File(“a.json”).getPath() = a.json
new File(“a.json”).exists() = false
System.getProperty(“user.dir”) = F:\work\test-filepath

可以看出,

  • AppMain.class.getResource 當前class文件的target位置
  • AppMain.class.getClassLoader().getResource 項目target位置
  • new File 項目位置
  • System.getProperty 項目位置

二、jar包運行情況

打成jar包
這次jar包在桌面:
F:\Users_F\zzzgd\Destop
結果:
在這裏插入圖片描述

三、總結

  • AppMain.class.getResource 當前class文件的target位置
  • AppMain.class.getClassLoader().getResource 項目target位置
  • new File 項目位置
  • System.getProperty 項目位置

可以看出,如果你是要獲取resources文件夾下的文件,使用第二種方式
如果要獲取某個文件,在項目根目錄和src平齊的,可以使用三四方式

發佈了145 篇原創文章 · 獲贊 193 · 訪問量 59萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章