一、获取当前项目路径
String projectPath = System.getProperty("user.dir");
System.out.println("当前项目路径:" + projectPath);
二、获取类路径下的资源路径
Class clazz = Main.class;
String resourcePath = clazz.getResource("/").getPath();
System.out.println("资源路径:" + resourcePath);
三、获取指定类的加载路径
ClassLoader classLoader = Main.class.getClassLoader();
String classLoaderPath = classLoader.getResource("").getPath();
System.out.println("加载路径:" + classLoaderPath);
四、使用 Paths 类来处理路径
import java.nio.file.Paths;
String path = Paths.get("dir", "file.txt").toString();
System.out.println("路径:" + path);