Java 读取 JAR 包中的文件
在 Java 中读取 JAR 包内的文件有几种常用方法,以下是详细的实现方式:

使用 ClassLoader.getResourceAsStream()
这是最常用的方法,适用于读取 JAR 包中的资源文件:
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JarFileReader {
public static void main(String[] args) {
String fileName = "config.properties"; // JAR包中的文件路径
try (InputStream inputStream = JarFileReader.class.getClassLoader()
.getResourceAsStream(fileName)) {
if (inputStream == null) {
System.out.println("文件未找到: " + fileName);
return;
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用 JarFile 类直接读取 JAR
如果需要更精细的控制,可以直接操作 JAR 文件:
import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.io.InputStream;
public class DirectJarReader {
public static void main(String[] args) {
String jarPath = "path/to/your.jar"; // JAR文件路径
String fileName = "config.properties"; // JAR中的文件路径
try (JarFile jarFile = new JarFile(jarPath)) {
JarEntry entry = jarFile.getJarEntry(fileName);
if (entry == null) {
System.out.println("文件未找到: " + fileName);
return;
}
try (InputStream inputStream = jarFile.getInputStream(entry)) {
// 读取输入流内容...
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用 Java 7+ 的 NIO API
Java 7 及以上版本可以使用 NIO API:
import java.nio.file.*;
import java.io.IOException;
import java.io.InputStream;
public class NioJarReader {
public static void main(String[] args) {
String jarPath = "path/to/your.jar";
String fileName = "config.properties";
try (FileSystem fs = FileSystems.newFileSystem(Paths.get(jarPath), null)) {
Path path = fs.getPath(fileName);
if (!Files.exists(path)) {
System.out.println("文件未找到: " + fileName);
return;
}
try (InputStream inputStream = Files.newInputStream(path)) {
// 读取输入流内容...
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意事项
- 文件路径:JAR 包中的文件路径使用正斜杠 作为分隔符,而不是反斜杠
\ - 资源位置:如果文件在 JAR 的根目录,直接使用文件名;如果在子目录,使用
subdir/filename - 类加载器:
getResourceAsStream()查找资源的顺序是:- 类的包路径
- 类加载器的根路径
- 文件编码:读取文本文件时要注意编码问题,建议使用
InputStreamReader指定编码 - JAR 内部 vs 外部:方法二和方法三适用于读取外部 JAR 文件,方法一适用于读取当前 JAR 或类路径中的资源
示例:读取当前 JAR 中的文本文件
public class ReadTextFromJar {
public static String readTextFile(String fileName) throws IOException {
try (InputStream inputStream = ReadTextFromJar.class
.getClassLoader()
.getResourceAsStream(fileName)) {
if (inputStream == null) {
throw new IOException("文件未找到: " + fileName);
}
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
public static void main(String[] args) {
try {
String content = readTextFile("textfile.txt");
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法可以根据你的具体需求选择使用,方法一最简单,方法二和方法三提供了更多的灵活性。


