简体   繁体   English

尝试在netbeans中读取文件时找不到文件异常

[英]file not found exception while trying to read file in netbeans

I am making an simple application to play sounds in Java. 我正在制作一个简单的应用程序来播放Java声音。 I am able to do that when I keep the audio files in D: disk. 当我将音频文件保存在D:磁盘中时,我能够做到这一点。 Here is the code 下面是代码

in = new FileInputStream("D:\\"+selectedSounds[position]+".wav");
//Some code for playing audio

Then I placed the audio files in same package where the Jframe class is present. 然后,我将音频文件放在存在Jframe类的同一包中。 But when I run it prompts fileNotFound exception. 但是,当我运行它时,会提示fileNotFound异常。 Can some one tell me why this is happening. 有人可以告诉我为什么会这样。

in = new FileInputStream(selectedSounds[position]+".wav");
// I have also tried 
    new FileInputStream("./"+selectedSounds[position]+".wav");

Here is the file path 这是文件路径

在此处输入图片说明

Your wave file, contained within the "Source Packages" won't be accessible once the program is packaged as a Jar, as the files will be embedded within the Jar itself and no longer accessible as files. 一旦将程序打包为Jar,将无法访问包含在“源包”中的wave文件,因为这些文件将嵌入到Jar本身中,并且不再作为文件访问。

Instead, you should be using Class#getResourceAsStream , for example... 相反,您应该使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM