[英]Java.io.FileNotFoundException for a file that's there
I am currently creating a err... annoying program that repeatedly plays a ding sound, but there is a error whenever I run it. 我当前正在创建一个错误的...烦人的程序,该程序反复播放叮叮当当的声音,但是每当我运行它时都会出错。 I have tried everything, and the file IS in the correct spot. 我已经尝试了一切,并且文件位于正确的位置。 Here is my current code: 这是我当前的代码:
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);
}
}
}
And yes, I have used other formats of the code like //src//ding.wav
Any help would be appreciated. 是的,我使用了其他格式的代码,例如//src//ding.wav
将不胜感激。
EDIT: also the error is 编辑:也是错误
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: The error is older before I renamed the file. EDIT2:在我重命名文件之前,该错误是较旧的。 I changed the error to reflect the current filename 我更改了错误以反映当前文件名
I think you're trying to read a resource in a jar
file specifying the absolute path of your hard disk drive. 我认为您正在尝试读取jar
文件中的资源,该文件指定了硬盘驱动器的绝对路径。
ding.jar\src\hit.wav
So there are two alternatives, or unzip your ding.jar file into a directory. 因此,有两种选择,或者将ding.jar文件解压缩到目录中。 Or specify the relative path and access to the file using the classloader resource reader. 或使用类加载器资源读取器指定相对路径和对文件的访问。
Using the resource reader you can find to the hit.wav
file using 使用资源阅读器,您可以使用以下内容找到hit.wav
文件
InputStream in = PlaySound.class.getResource("/ding.wav").openStream();
AudioStream audioStream = new AudioStream(in);
You probably need to unjar your ding.jar to expand its file structure. 您可能需要解压缩ding.jar才能扩展其文件结构。 FileInputStream (probably?) can't access the jar'ed version of this file. FileInputStream(可能是?)无法访问此文件的jared版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.