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

发布时间 2023-12-18 00:08:57作者: 清清飞扬

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/