前言
资源管理是开发过程中不可或缺的一部分。资源文件,如配置文件、图片和文本文件,通常被放置在项目的resources目录下,以便于管理和访问。然而,对于初学者来说,如何正确地读取这些文件路径可能会遇到一些困惑。本文将深入探讨java中读取resources目录下文件路径的几种常见方法,帮助开发者更有效地管理项目资源,并确保应用程序的健壮性和可维护性。
代码一:根据文件路径读取文件内容
/** * 根据文件路径读取文件内容 * @param fileinpath * @throws ioexception */ public static void getfilecontent(object fileinpath) throws ioexception { bufferedreader br = null; if (fileinpath == null) { return; } if (fileinpath instanceof string) { br = new bufferedreader(new filereader(new file((string) fileinpath))); } else if (fileinpath instanceof inputstream) { br = new bufferedreader(new inputstreamreader((inputstream) fileinpath)); } string line; while ((line = br.readline()) != null) { system.out.println(line); } br.close(); }
代码二:使用getresource和getpath方法
这里的getresource(“”)里面是空字符串
public void function1(string filename) throws ioexception { string path = this.getclass().getclassloader().getresource("").getpath(); //注意getresource("")里面是空字符串 system.out.println(path); string filepath = path + filename; system.out.println(filepath); getfilecontent(filepath); }
代码三:使用getresource和getpath方法
直接通过getresource(filename)方法获取文件路径,如果路径中带有中文要使用urldecoder.decode进行解码。
/** * 直接通过文件名getpath来获取路径 * @param filename * @throws ioexception */ public void function2(string filename) throws ioexception { string path = this.getclass().getclassloader().getresource(filename).getpath();//注意getresource("")里面是空字符串 system.out.println(path); string filepath = urldecoder.decode(path, "utf-8");//如果路径中带有中文会被urlencoder,因此这里需要解码 system.out.println(filepath); getfilecontent(filepath); }
代码四:通过文件名+getfile()来获取文件
文件路径的话getfile和getpath效果是一样的,如果是url路径的话getpath是带有参数的路径。如下所示:
url.getfile()=/pub/files/foobar.txt?id=zhouzhou url.getpath()=/pub/files/foobar.txt
使用getfile()方式获取文件的代码如下:
/** * 直接通过文件名+getfile()来获取 * @param filename * @throws ioexception */ public void function3(string filename) throws ioexception { string path = this.getclass().getclassloader().getresource(filename).getfile();//注意getresource("")里面是空字符串 system.out.println(path); string filepath = urldecoder.decode(path, "utf-8");//如果路径中带有中文会被urlencoder,因此这里需要解码 system.out.println(filepath); getfilecontent(filepath); }
代码五:使用getresourceasstream方法获取流(无需文件路径)
springboot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/** * 直接使用getresourceasstream方法获取流 * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件 * @param filename * @throws ioexception */ public void function4(string filename) throws ioexception { inputstream in = this.getclass().getclassloader().getresourceasstream(filename); getfilecontent(in); }
使用getresourceasstream方法获取流,不使用getclassloader可以使用getresourceasstream(“/配置测试.txt”)直接从resources根路径下获取,springboot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/** * 直接使用getresourceasstream方法获取流 * 如果不使用getclassloader,可以使用getresourceasstream("/配置测试.txt")直接从resources根路径下获取 * @param filename * @throws ioexception */ public void function5(string filename) throws ioexception { inputstream in = this.getclass().getresourceasstream("/" + filename); getfilecontent(in); }
代码六:通过classpathresource类获取文件流
/** * 通过classpathresource类获取,建议springboot中使用 * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件 * @param filename * @throws ioexception */ public void function6(string filename) throws ioexception { classpathresource classpathresource = new classpathresource(filename); inputstream inputstream = classpathresource.getinputstream(); getfilecontent(inputstream); }
代码七:通过绝对路径获取项目中文件的位置
/** * 通过绝对路径获取项目中文件的位置(不能用于服务器) * @param filename * @throws ioexception */ public void function7(string filename) throws ioexception { string rootpath = system.getproperty("user.dir");//e:\workspace\git\spring-framework-learning-example string filepath = rootpath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+filename; getfilecontent(filepath); }
代码八:new file(“”)获取当前的绝对路径
/** * 通过绝对路径获取项目中文件的位置(不能用于服务器) * @param filename * @throws ioexception */ public void function8(string filename) throws ioexception { //参数为空 file directory = new file(""); //规范路径:getcanonicalpath() 方法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉 string rootcanonicalpath = directory.getcanonicalpath(); //绝对路径:getabsolutepath() 方法返回文件的绝对路径,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 file 对象时的路径 string rootabsolutepath =directory.getabsolutepath(); system.out.println(rootcanonicalpath); system.out.println(rootabsolutepath); string filepath = rootcanonicalpath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+filename; getfilecontent(filepath); }
代码九:设置环境变量
主要是通过设置环境变量,将文件放在环境变量中,原理也是通过绝对路径获取。
设置一个环境变量:test_root=d:\workspace\git\spring-framework-learning-example
system.getenv("test_root"); system.getproperty("test_root")
通过设置环境变量的方式,然后通过绝对路径获取文件
/** * 通过绝对路径获取项目中文件的位置 * @param filename * @throws ioexception */ public void function9(string filename) throws ioexception { system.setproperty("test_root","e:\\workspace\\git\\spring-framework-learning-example"); //参数为空 string rootpath = system.getproperty("test_root"); system.out.println(rootpath); string filepath = rootpath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+filename; getfilecontent(filepath); }
总结
到此这篇关于java读取resources目录下文件路径的九种代码示例的文章就介绍到这了,更多相关java读取resources目录下文件路径内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论