在项目开发中,经常会有一些静态资源放在resources目录下,和项目一起打包。当需要使用代码时,通过读取文件的方式加载使用;本文总结了九种获取资源目录下文件的方法。打印文件的常用方法如下:/***根据文件路径读取文件内容**@paramfileInPath*@throwsIOException*/publicstaticvoidgetFileContent(ObjectfileInPath)throwsIOException{BufferedReaderbr=null;如果(fileInPath==null){返回;}if(fileInPathinstanceofString){br=newBufferedReader(newFileReader(newFile((String)fileInPath)));}elseif(fileInPathinstanceofInputStream){br=newBufferedReader(newInputStreamReader((InputStream)fileInPath));}串线;while((line=br.readLine())!=null){System.out.println(line);}br.close();}推荐一个开源免费的SpringBoot最全教程:https://github.com/javastacks/spring-boot-best-practice方法1主要核心方法是使用getResource和getPath方法,其中getResource("")包含一个空字符串publicvoidfunction1(StringfileName)throwsIOException{Stringpath=this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")包含一个空字符串System.out.println(path);字符串文件路径=路径+文件名;System.out.println(文件路径);getFileContent(filePath);}第二个主要核心方法是使用getResource和getPath方法,通过getResource(fileName)方法直接获取文件路径。注意如果是路径中文必须用URLDecoder.decode解码/***直接通过文件名getPath获取路径**@paramfileName*@throwsIOException*/publicvoidfunction2(StringfileName)throwsIOException{Stringpath=this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")包含一个空字符串System.out.println(path);StringfilePath=URLDecoder.decode(path,"UTF-8");//如果路径中有中文,就是URLEncoder,所以这里需要解码System.out.println(filePath);getFileContent(filePath);}方法三通过文件名+getFile()直接获取文件。如果是文件路径,getFile和getPath的作用是一样的,如果是URL路径,getPath是带参数的路径。如下图:url.getFile()=/pub/files/foobar.txt?id=123456url.getPath()=/pub/files/foobar.txt使用getFile()获取文件的代码如下:/***直接通过文件名+getFile()获取**@paramfileName*@throwsIOException*/publicvoidfunction3(StringfileName)throwsIOException{Stringpath=this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")中有一个空字符串System.out.println(path);StringfilePath=URLDecoder.decode(path,"UTF-8");//如果路径中有中文是URLEncoder,需要解码System.out.println(filePath);getFileContent(filePath);}方法四(重要)直接使用getResourceAsStream方法获取流。以上方法都需要获取文件路径,但是在SpringBoot中所有的文件都在jar包中,没有实际路径,所以可以使用下面的方法。/***直接使用getResourceAsStream方法获取流*springboot项目中需要使用该方法,因为jar包中没有实际存放文件的路径**@paramfileName*@throwsIOException*/publicvoidfunction4(字符串文件名)抛出IOException{InputStreamin=this.getClass().getClassLoader().getResourceAsStream(fileName);getFileContent(in);}方法五(重要)主要是使用getResourceAsStream方法获取流。如果不使用getClassLoader,可以直接从资源根路径使用getResourceAsStream("/configurationtest.txt"),SpringBoot中所有文件都在jar包中,没有实际路径,所以下面的方法可以使用。/***直接使用getResourceAsStream方法获取流*如果不使用getClassLoader,可以使用getResourceAsStream("/configurationtest.txt")直接从资源根路径获取流**@paramfileName*@throwsIOException*/publicvoidfunction5(StringfileName)throwsIOException{InputStreamin=this.getClass().getResourceAsStream("/"+fileName);getFileContent(in);}最新Spring面试题整理:https://www.javastack.cn/spring-mst/method6(重要)通过ClassPathResource类获取文件流。SpringBoot中的所有文件都在jar包中,并没有实际路径,所以可以使用下面的方法。/***通过ClassPathResource类获取,推荐在SpringBoot中使用*springboot项目中需要使用该方法,因为jar包中没有实际存放文件的路径**@paramfileName*@throwsIOException*/publicvoidfunction6(StringfileName)throwsIOException{ClassPathResourceclassPathResource=newClassPathResource(fileName);InputStreaminputStream=classPathResource.getInputStream();getFileContent(inputStream);}方法七通过绝对路径获取文件在项目中的位置,这只是本地的绝对路径,不能在服务器上获取。/***通过绝对路径获取文件在项目中的位置(服务器端不能使用)*@paramfileName*@throwsIOException*/publicvoidfunction7(StringfileName)throwsIOException{StringrootPath=System.getProperty("user.dir");//E:\WorkSpace\Git\spring-framework-learning-exampleStringfilePath=rootPath+"\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+文件名;getFileContent(filePath);}方法8通过newFile("")获取当前绝对路径,这只是本地绝对路径,不能用于服务器获取。/***通过绝对路径获取文件在项目中的位置(不能用于服务端)*@paramfileName*@throwsIOException*/publicvoidfunction8(StringfileName)throwsIOException{//参数为空文件目录=新文件(“”);//指定路径:getCanonicalPath()方法返回绝对路径,会解析..\、.\等符号StringrootCanonicalPath=directory.getCanonicalPath();//绝对路径:getAbsolutePath()方法返回文件如果构建时是完整路径,则直接返回完整路径;如果构造时是相对路径,则返回当前目录的路径+构造File对象时的路径StringrootAbsolutePath=directory.getAbsolutePath();系统输出。println(rootCanonicalPath);System.out.println(rootAbsolutePath);StringfilePath=rootCanonicalPath+"\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;getFileContent(filePath);}第九种方法主要是设置环境变量,将文件放到环境变量中。原理也是通过绝对路径获取。在示例中,我设置了一个环境变量:TEST_ROOT=E:\\WorkSpace\\Git\\spring-framework-learning-exampleSystem.getenv("TEST_ROOT");System.getProperty("TEST_ROOT")通过设置环境变量的方法,然后通过绝对路径获取文件/***通过绝对路径获取文件在项目中的位置**@paramfileName*@throwsIOException*/publicvoidfunction9(StringfileName)throwsIOException{System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example");//参数为空StringrootPath=System.getProperty("TEST_ROOT");System.out.println(rootPath);StringfilePath=rootPath+"\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;getFileContent(filePath);}版权声明:本文为CSDN博主“leo825...”原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。
