我在com.nishu.ld28.utilities包的类中有一个FileInputStream,我想访问Sounds文件夹中的声音文件,该文件夹不在com.nishu.ld28包中。我指定加载路径,如下所示:
"sounds/merry_xmas.wav"
然后尝试像这样加载它:
new BufferedInputStream(new FileInputStream(path))
导出jar时,我通过命令行运行的提示符提示找不到文件。我在Eclipse中运行程序时知道如何访问文件,但是导出时我不知道如何将FileInputStream指向Sounds文件夹。
编辑:根据要求,这是我的代码:
public void loadSound(String path) {
WaveData data = null;
data = WaveData.create(GameSound.class.getClassLoader().getResourceAsStream(path));
int buffer = alGenBuffers();
alBufferData(buffer, data.format, data.data, data.samplerate);
data.dispose();
source = alGenSources();
alSourcei(source, AL_BUFFER, buffer);
}
WaveData接受InputStream或其他类型的IO。
最佳答案
我将com.nishu.ld28.utilities
放在类的同一包中,我们将其称为MyClass。
您的包裹:
您的代码:
package com.nishu.ld28.utilities;
import java.io.InputStream;
public class MyClass {
public static void main(String[] args) {
InputStream is = MyClass.class.getResourceAsStream("sound/merry_xmas.wav");
System.out.format("is is null ? => %s", is==null);
}
}
输出量
is is null ? => false
关于java - FileInputStream进入Jar的根目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20612971/