在Java中获取resource目录路径有多种方法,具体取决于你的项目结构和资源文件的位置。以下是一些常见的方法:
URL resourceUrl = getClass().getClassLoader().getResource("your/resource/file.txt"); String resourcePath = resourceUrl.getPath(); System.out.println(resourcePath); URL resourceUrl = getClass().getResource("/your/resource/file.txt"); String resourcePath = resourceUrl.getPath(); System.out.println(resourcePath); String resourcePath = System.getProperty("user.dir") + "/src/main/resources/your/resource/file.txt"; System.out.println(resourcePath); File file = new File("src/main/resources/your/resource/file.txt"); String resourcePath = file.getAbsolutePath(); System.out.println(resourcePath); 根据你的项目结构和资源文件的位置选择适合的方法来获取resource目录路径。