如何将jar中的文本文件复制到jar外的文件中? [英] How do I copy a text file from a jar into a file outside of the jar?

查看:198
本文介绍了如何将jar中的文本文件复制到jar外的文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的jar中包含com.test.io的文件中有一个名为test.txt的文件。

Let's say I have a file called test.txt within the package "com.test.io" within my jar.

我将如何编写课程检索此文本文件,然后将内容复制到文件系统上的新文件?

How would I go about writing a class which retrieves this text file and then copies the contents to a new file on the file system?

推荐答案

假设所说的jar在你的classpath:

Assuming said jar is on your classpath:

URL url = getClassLoader().getResource("com/test/io/test.txt");
FileOutputStream output = new FileOutputStream("test.txt");
InputStream input = url.openStream();
byte [] buffer = new byte[4096];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
    output.write(buffer, 0, bytesRead);
    bytesRead = input.read(buffer);
}
output.close();
input.close();

这篇关于如何将jar中的文本文件复制到jar外的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆