getCanonicalPath(); //绝对路径:getAbsolutePath() 方法返回文件的绝对路径,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径 String rootAbsolutePath =directory.getAbsolutePath(); System.out.println(rootCanonicalPath); System.out.println(root...
使用ClassLoader的getResource方法: 通过ClassLoader的getResource方法,可以获取资源文件的URL,然后从中提取路径。 示例代码: java URL resourceUrl = getClass().getClassLoader().getResource(""); String resourcePath = resourceUrl.getPath(); System.out.println("Resource Directory: " + resourcePath); ...
publicclassResourceDirectoryExample{publicstaticvoidmain(String[]args){ClassLoaderclassLoader=ResourceDirectoryExample.class.getClassLoader();URLurl=classLoader.getResource("resources/");Stringpath=url.getPath();System.out.println("Resources directory path: "+path);}} 1. 2. 3. 4. 5. 6. 7. 8. ...
1:获取resource目录: LibXIStream.class.getClass().getResource("/").getPath() 2:获取resource目录下的文件输入流: GlobalUtils.class.getResourceAsStream("/dll/" + fileName); 3:resource目录不编译导致无法读取文件: 在pom.xml文件中加入以下内容即可 <build> <resources> <resource> <directory>src/main...
主要核心方法是使用getResource和getPath方法,直接通过getResource(fileName)方法获取文件路径,注意如果是路径中带有中文一定要使用URLDecoder.decode解码。 /** * 直接通过文件名getPath来获取路径 * *@paramfileName *@throwsIOException */publicvoidfunction2(String fileName)throwsIOException {Stringpath=this.getCla...
{// 获取资源目录的路径PathresourcePath=Paths.get("src/main/resources");try{if(Files.exists(resourcePath)){System.out.println("Resource directory exists: "+resourcePath.toAbsolutePath());}else{System.out.println("Resource directory does not exist.");}}catch(Exceptionex){ex.printStackTrace();...
getFileContent(filePath); } 方式二 主要核心方法是使用getResource和getPath方法,直接通过getResource(fileName)方法获取文件路径,注意如果是路径中带有中文一定要使用URLDecoder.decode解码。 /** * 直接通过文件名getPath来获取路径 * * @param fileName ...
5.当打成jar包时,获取resource⽬录下的⽂件,使⽤getResource()时会出现 FileNotFoundException:/opt/supplier_web/supplier_web.jar!/BOOT-INF/lib/supplier_web.web-0.0.1.jar!/font/simsun.ttf (No such file or directory)。这是因为打包后Spring试图访问⽂件系统路径,但⽆法访问JAR中的路径...
???File?f2?=?new?File(this.getClass().getResource("").getPath());???System.out.println(f2);???//?第二种:获取项目路径???D:\git\daotie\daotie???File?directory?=?new?File("");//?参数为空???String?courseFile?=?directory.getCanonicalPath();???System.out.println(c...
String fileUtl = this.getClass().getResource("文件名").getFile(); 示例路径结果:/E:/idea_work/sofn-qry-web/target/classes/CityJson.js 2. File directory = new File("");//参数为空 String courseFile = directory.getCanonicalPath()//标准的路径 ; ...