完美百科新手进阶玩家心得完美攻略

您现在的位置:首页 > 完美资讯 > 玩家心得

中提供了九种方式获取resources目录下文件的方式

来源:热血传奇私服专区 浏览次数: 发布时间:2022-11-28 08:46:10

概要

本文提供了在资源目录中检索文件的九种方法。以下是打印文件的方法:

/***基于文件路径读取文件内容**@paramfileInPath*@throwsIOException*/publicstaticvoidgetFileContent(ObjectfileInPath)throwsIOException{BufferedReaderbr=null; if(fileInPath==null){return; }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();}方式一

主要的核心方法是使用 getResource 和 getPath 方法,其中 getResource(“”) 是一个空字符串

publicvoidfunction1(StringfileName)throwsIOException{Stringpath=this.getClass().getClassLoader().getResource(“”).getPath();//NotegetResource(“”) 里面是空字符串System.out.println(path);字符串文件路径=路径+文件名;System.out.println(filePath);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() 获取文件的代码如下:

/*** Get **@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);} 方法 4(重要)。

直接使用 getResourceAsStream 方法获取流,以上几种方式都需要获取文件路径,但是在 SpringBoot 中所有文件都在 jar 包中剑灵男灵炎代码存放路径,没有实际路径,所以可以使用以下方式。

剑灵男灵炎代码存放路径_剑灵灵炎仙魔技能_剑灵灵炎

/*** 直接使用 getResourceAsStream 方法获取流 *Springboot 项目需要此方法,因为 jar 包中没有实际路径来保存文件 **@paramfileName*@throwsIOException*/publicvoidfunction4(StringfileName) throwsIOException{InputStreamin=this.getClass().getClassLoader().getResourceAsStream(fileName); getFileContent(in); }方式5(重要)。

主要用途

getResourceAsStream 方法获取流,没有 getClassLoader 你可以使用 getResourceAsStream(“/configuration test .txt”)直接从资源根路径获取,SpringBoot 中的所有文件都在 jar 包中,没有实际路径,所以可以使用以下方式。/*** 使用 getResourceAsStream

方法直接获取流 * 如果不使用 getClassLoader,则可以使用 getResourceAsStream(“/Configuration Test.txt”) get **@paramfileName*@throwsIOException*/publicvoidfunction5(StringfileName)throwsIOException{InputStreamin=this.getClass().getResourceAsStream(“/”+ fileName); getFileContent(in); }方式六(重要)。

剑灵男灵炎代码存放路径_剑灵灵炎仙魔技能_剑灵灵炎

通过 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\\”+fileName; getFileContent(filePath); }方式八

剑灵灵炎仙魔技能_剑灵男灵炎代码存放路径_剑灵灵炎

通过 new File(“) 获取当前绝对路径,仅获取本地绝对路径,不用于服务器获取。

/***通过绝对路径获取项目中文的位置(不能用于服务器)*@paramfileName*@throwsIOException*/publicvoidfunction8(StringfileName)throwsIOException{//Parameter is emptyFiledirectory=newFile(“”);// 规范路径: getCanonicalPath() 方法返回一个绝对路径,并将 : \, .\ 这样的符号解析出 StringrootCanonicalPath=directory.getCanonicalPath();//Absolute path: getAbsolutePath() 方法返回文件的绝对路径,如果它是一个完整的 path 在构造时,会直接返回 full 路径,如果是构造时的相对路径,则会返回当前目录的路径 + 构造 File 对象的路径 StringrootAbsolutePath=directory.getAbsolutePath();System.out.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-example

System.getenv(“TEST_ROOT”);System.getProperty(“TEST_ROOT”)通过

设置环境变量,然后通过绝对路径获取文件