简体   繁体   中英

file not found exception while trying to read file in netbeans

I am making an simple application to play sounds in Java. I am able to do that when I keep the audio files in D: disk. 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. But when I run it prompts fileNotFound exception. 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.

Instead, you should be using Class#getResourceAsStream , for example...

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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