package com.zgd.demo.file.path;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
/**
* AppMain
*
* @author zgd
* @date 2020/2/18 15:03
*/
public class AppMain {
public static void main(String[] args) throws MalformedURLException {
System.out.println("---------getResource---------");
//获取当前文件所在的路径
URL u1 = AppMain.class.getResource("");
//获取当前文件所在的路径
URL u2 = AppMain.class.getResource("a.json");
//获取项目根目录
URL u3 = AppMain.class.getResource("/");
URL u4 = AppMain.class.getResource("./a.json");
URL u5 = AppMain.class.getResource("/a.json");
System.out.println("AppMain.class.getResource(\"\").getPath() = " + (u1 == null ? "NullPointerException" : u1.getPath()));
System.out.println("AppMain.class.getResource(\"a.json\").getPath() = " + (u2 == null ? "NullPointerException" : u2.getPath()));
System.out.println("AppMain.class.getResource(\"/\").getPath() = " + (u3 == null ? "NullPointerException" : u3.getPath()));
System.out.println("AppMain.class.getResource(\"./a.json\").getPath() = " + (u4 == null ? "NullPointerException" : u4.getPath()));
System.out.println("AppMain.class.getResource(\"/a.json\").getPath() = " + (u5 == null ? "NullPointerException" : u5.getPath()));
System.out.println("\n---------getClassLoader---------");
String cl = Optional.ofNullable(AppMain.class.getClassLoader().getResource("")).orElse(new URL("file://NullPointerException")).getPath();
String cl2 = Optional.ofNullable(AppMain.class.getClassLoader().getResource("a.json")).orElse(new URL("file://NullPointerException")).getPath();
String cl3 = Optional.ofNullable(AppMain.class.getClassLoader().getResource("./a.json")).orElse(new URL("file://NullPointerException")).getPath();
String cl4 = Optional.ofNullable(AppMain.class.getClassLoader().getResource("/")).orElse(new URL("file://NullPointerException")).getPath();
System.out.println("AppMain.class.getClassLoader().getResource(\"\").getPath() = " + (cl == null ? "NullPointerException" : cl));
System.out.println("AppMain.class.getClassLoader().getResource(\"a.json\").getPath() = " + (cl2 == null ? "NullPointerException" : cl2));
System.out.println("AppMain.class.getClassLoader().getResource(\"./a.json\").getPath() = " + (cl3 == null ? "NullPointerException" : cl3));
System.out.println("AppMain.class.getClassLoader().getResource(\"/\").getPath() = " + (cl4 == null ? "NullPointerException" : cl4));
System.out.println("\n---------getPath---------");
String f1 = new File("").getPath();
String f2 = new File("a.json").getPath();
String f3 = new File("./a.json").getPath();
String f4 = new File("/").getPath();
System.out.println("new File(\"\").getPath() = " + f1);
System.out.println("new File(\"a.json\").getPath() = " + f2);
System.out.println("new File(\"./a.json\").getPath() = " + f3);
System.out.println("new File(\"/\").getPath() = " + f4);
System.out.println("\n---------getAbsolutePath---------");
//当前工程的绝对路径
String absolutePath1 = new File("").getAbsolutePath();
String absolutePath2 = new File("a.json").getAbsolutePath();
String absolutePath3 = new File("./a.json").getAbsolutePath();
String absolutePath4 = new File("/").getAbsolutePath();
System.out.println("new File(\"\").getAbsolutePath() = " + absolutePath1);
System.out.println("new File(\"a.json\").getAbsolutePath() = " + absolutePath2);
System.out.println("new File(\"./a.json\").getAbsolutePath() = " + absolutePath3);
System.out.println("new File(\"/\").getAbsolutePath() = " + absolutePath4);
// 获取工程路径
System.out.println("\n---------user.dir---------");
String sp1 = System.getProperty("user.dir");
System.out.println("System.getProperty(\"user.dir\") = " + sp1);
System.out.println("\n---------getFile---------");
try {
FileInputStream fileInputStream = new FileInputStream(AppMain.class.getClassLoader().getResource("a.json").getPath());
if (fileInputStream != null){
System.out.println("AppMain.class.getClassLoader().getResource(\"a.json\")获取文件成功");
}else{
System.out.println("AppMain.class.getClassLoader().getResource(\"a.json\")未获取到文件");
}
} catch (Exception e) {
System.out.println("AppMain.class.getClassLoader().getResource(\"a.json\")获取文件失败 = " + e);
}
try {
InputStream fileInputStream = AppMain.class.getClassLoader().getResourceAsStream("a.json");
if (fileInputStream != null){
System.out.println("AppMain.class.getClassLoader().getResourceAsStream(\"a.json\")获取文件成功");
}else{
System.out.println("AppMain.class.getClassLoader().getResourceAsStream(\"a.json\")未获取到文件");
}
} catch (Exception e) {
System.out.println("AppMain.class.getClassLoader().getResourceAsStream(\"a.json\")获取文件失败 = " + e);
}
}
}
|