Java.io.FileNotFoundException 对于那里的文件

Java.io.FileNotFoundException for a file that's there

我目前正在创建一个 err... 烦人的程序,它反复播放叮当声,但每当我 运行 它时都会出错。我什么都试过了,文件在正确的位置。这是我当前的代码:

public class PlaySound {

public static void main(String[] args) throws Exception {
    while (true) {
            String path = PlaySound.class.getProtectionDomain().getCodeSource().getLocation().getPath().replaceAll("%20", " ");;
            InputStream in = new FileInputStream(path + "//src//ding.wav");

            AudioStream audioStream = new AudioStream(in);

            AudioPlayer.player.start(audioStream);
            TimeUnit.SECONDS.sleep(1);
        }
    }
}

是的,我使用了其他格式的代码,例如 //src//ding.wav 任何帮助,将不胜感激。

编辑:错误也是

Exception in thread "main" java.io.FileNotFoundException: C:\Users\*** ******\Desktop\ding.jar\src\ding.wav (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at net.lookatthis.PlaySound.main(PlaySound.java:19)

EDIT2:在我重命名文件之前,错误是旧的。我更改了错误以反映当前文件名

我认为您正在尝试读取 jar 文件中指定硬盘驱动器绝对路径的资源。

ding.jar\src\hit.wav

所以有两种选择,或者将您的 ding.jar 文件解压缩到一个目录中。 或者使用类加载器资源指定相对路径和对文件的访问 reader.

使用资源 reader 您可以使用

找到 hit.wav 文件
InputStream in = PlaySound.class.getResource("/ding.wav").openStream();
AudioStream audioStream = new AudioStream(in);

您可能需要解压缩 ding.jar 以扩展其文件结构。 FileInputStream(可能?)无法访问此文件的 jar 版本。