随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

Java实现读取resources目录下的文件路径的九种方式

From: https://www.cnblogs.com/sunny3158/p/17818827.html

Java实现读取resources目录下的文件路径通常有以下九种方式:

1. 使用ClassLoader的getResource()方法

在Java中,可以使用ClassLoader的getResource()方法获取resources目录下的文件路径。示例代码如下:

URL resource = getClass().getClassLoader().getResource("example.txt");
String path = resource.getPath();
System.out.println(path);

2. 使用ClassLoader的getResourceAsStream()方法

除了使用getResource()方法,还可以使用ClassLoader的getResourceAsStream()方法获取资源流并读取资源内容。示例代码如下:

InputStream inputStream = getClass().getClassLoader().getResourceAsStream("example.txt");
String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
System.out.println(content);

3. 使用ClassLoader的getSystemResource()方法

使用ClassLoader的getSystemResource()方法可以直接从系统路径获取资源,示例代码如下:

URL resource = ClassLoader.getSystemResource("example.txt");
String path = resource == null ? "" : resource.getPath();
System.out.println(path);

4. 使用ClassLoader的getSystemResourceAsStream()方法

和第2种方式类似,ClassLoader的getSystemResourceAsStream()方法可以获取资源流并读取资源内容。示例代码如下:

InputStream inputStream = ClassLoader.getSystemResourceAsStream("example.txt");
String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
System.out.println(content);

5. 使用ClassLoader的getResourceAsStream()方法结合Properties类读取属性文件

在Java中,可以使用Properties类读取资源目录下的属性文件。示例代码如下:

InputStream inputStream = getClass().getClassLoader().getResourceAsStream("example.properties");
Properties properties = new Properties();
properties.load(inputStream);
String propertyValue = properties.getProperty("example.key");

System.out.println(propertyValue);

 

6. 使用java.io.FileInputStream读取文件

除了使用ClassLoader,还可以使用java.io.FileInputStream读取文件。示例代码如下:

 

File file = new File("src/main/resources/example.txt");
FileInputStream fis = new FileInputStream(file);
byte[] fileContent = fis.readAllBytes();
String content = new String(fileContent, StandardCharsets.UTF_8);
System.out.println(content);

7. 使用java.nio.file.Path读取文件

Java7中引入了java.nio.file.Path类,也可以使用该类读取文件。示例代码如下:

Path path = Paths.get(getClass().getClassLoader().getResource("example.txt").toURI());
byte[] fileContent = Files.readAllBytes(path);
String content = new String(fileContent, StandardCharsets.UTF_8);
System.out.println(content);

8. 使用spring的Resource类读取文件

如果项目中使用了Spring框架,可以使用spring的Resource类读取文件。示例代码如下:

Resource resource = new ClassPathResource("example.txt");
InputStream inputStream = resource.getInputStream();
String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
System.out.println(content);

9. 使用Guava的Resources类读取文件

Google开源的Guava库中也提供了读取资源文件的类Resources。示例代码如下:

URL resource = Resources.getResource("example.txt");
String fileContent = Resources.toString(resource, StandardCharsets.UTF_8);
System.out.println(fileContent);

以上就是Java实现读取resources目录下的文件路径的九种方式的完整攻略,希望对你有所帮助。

 

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java实现读取resources目录下的文件路径的九种方式 - Python技术站

原文链接:https://pythonjishu.com/yupleomvlryqmxz/

posted on   清清飞扬  阅读(11816)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示