java中 如何从jar中读取资源文件?

在Java中,从JAR文件中读取资源文件通常使用类加载器(ClassLoader)或者通过getClass().getResourceAsStream()方法。以下是几种常见的方法:

方法一:使用 getClass().getResourceAsStream()

这是最常见和推荐的方法,因为它简单且易于理解。

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadResourceFile {
    public static void main(String[] args) {
        // 假设资源文件名为 "config.properties",并且位于资源根目录
        String resourceName = "config.properties";
        
        // 使用类加载器获取资源文件的输入流
        try (InputStream inputStream = ReadResourceFile.class.getResourceAsStream(resourceName);
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

            if (inputStream == null) {
                System.out.println("Resource not found: " + resourceName);
                return;
            }

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方法二:使用 ClassLoader.getResourceAsStream()

你也可以直接使用类加载器来获取资源文件的输入流。

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadResourceFile {
    public static void main(String[] args) {
        // 假设资源文件名为 "config.properties",并且位于资源根目录
        String resourceName = "config.properties";
        
        // 获取当前类的类加载器
        ClassLoader classLoader = ReadResourceFile.class.getClassLoader();
        
        try (InputStream inputStream = classLoader.getResourceAsStream(resourceName);
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

            if (inputStream == null) {
                System.out.println("Resource not found: " + resourceName);
                return;
            }

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方法三:使用 Files.readAllLines (Java 7+)

如果你使用的是Java 7或更高版本,并且资源文件是文本文件,可以使用java.nio.file.Files类来读取文件内容。不过,请注意,这种方法通常用于文件系统路径,而不是JAR中的资源路径。对于JAR中的资源,还是推荐使用前两种方法。

java 复制代码
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class ReadResourceFile {
    public static void main(String[] args) {
        // 假设资源文件名为 "config.properties",并且位于资源根目录
        String resourceName = "/config.properties"; // 注意路径前的斜杠
        
        // 获取资源文件的URI
        try {
            List<String> lines = Files.readAllLines(Paths.get(ReadResourceFile.class.getResource(resourceName).toURI()));
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
相关推荐
顺风尿一寸3 小时前
从 Java NIO poll 到 Linux 内核 poll:一次系统调用的完整旅程
java
程途知微3 小时前
JVM运行时数据区各区域作用与溢出原理
java
华仔啊6 小时前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
xiaoye20188 小时前
Lettuce连接模型、命令执行、Pipeline 浅析
java
beata11 小时前
Java基础-18:Java开发中的常用设计模式:深入解析与实战应用
java·后端
Seven9711 小时前
剑指offer-81、⼆叉搜索树的最近公共祖先
java
雨中飘荡的记忆1 天前
保证金系统入门到实战
java·后端
Nyarlathotep01131 天前
Java内存模型
java
暮色妖娆丶1 天前
不过是吃了几年互联网红利罢了,我高估了自己
java·后端·面试