在当前jar之外复制资源

时间:2014-03-27 20:36:16

标签: java resources unzip

我有一个播放背景音乐的游戏。

我使用的mp3库不支持in-jar读取,所以我无法使用getClass().getResource()方法查找资源并将url传递给库,我必须提取该文件并将其路径传递给库。

我使用此方法将文件复制到jar外:

File f = new File("15.mp3"); //the music file for output

InputStream is = getClass().getResourceAsStream("15.mp3"); //the mp3 inside the jar
BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(f));
byte[] read = new byte[1024];
while (is.read(read) > -1) {
    buf.write(read);
    buf.flush();
}
buf.close();

这种方法有效,声音会被播放,但它有点沙哑(原版 - 存储在jar中 - 是完美的)。

我是否对副本做错了什么?我是否应该做其他事情来正确地复制jar包外的jar内资源?

0 个答案:

没有答案
相关问题