![](/img/trans.png)
[英]File not Found exception while trying to read file by using Apache-Camel
[英]file not found exception while trying to read file in netbeans
我正在制作一個簡單的應用程序來播放Java聲音。 當我將音頻文件保存在D:磁盤中時,我能夠做到這一點。 下面是代碼
in = new FileInputStream("D:\\"+selectedSounds[position]+".wav");
//Some code for playing audio
然后,我將音頻文件放在存在Jframe類的同一包中。 但是,當我運行它時,會提示fileNotFound異常。 有人可以告訴我為什么會這樣。
in = new FileInputStream(selectedSounds[position]+".wav");
// I have also tried
new FileInputStream("./"+selectedSounds[position]+".wav");
這是文件路徑
一旦將程序打包為Jar,將無法訪問包含在“源包”中的wave文件,因為這些文件將嵌入到Jar本身中,並且不再作為文件訪問。
相反,您應該使用Class#getResourceAsStream
,例如...
try (InputStream in = getClass().getResourceAsStream("/PlayAudio/" + selectedSounds[position]+".wav")) {
// You now have an InputStream to your resource, have fun
} catch (IOException | NullPointerException exp) {
exp.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.