gpt4 book ai didi

java - java.io.FileNotFoundException即使文件路径正确

转载 作者:行者123 更新时间:2023-12-03 01:15:09 25 4
gpt4 key购买 nike

我使用的代码:

package play;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Play {

InputStream music;
public Play() {
URL url=getClass().getResource("/music/Whitewoods - College Kill Dream.mp3");
System.out.println(url.toString());
try {
FileInputStream fileInputStream=new FileInputStream(new File(url.toString()));
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main(String args[]) {
new Play();

}



}

上面的代码下面的行:
System.out.println(url.toString());

prints :
file:/C:/Users/eclipse-workspace/audioboard/bin/music/Whitewoods%20-%20College%20Kill%20Dream.mp3
如果我直接将其复制并放在chrome的url放置框中。文件打开,但该行:
FileInputStream fileInputStream=new FileInputStream(new File(url.toString()));
给出文件未找到错误。
错误堆栈:
java.io.FileNotFoundException: file:\C:\Users\eclipse-workspace\audioboard\bin\music\Whitewoods%20-%20College%20Kill%20Dream.mp3 (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
at play.Play.<init>(Play.java:17)
at play.Play.main(Play.java:26)
感谢您的帮助。

最佳答案

您可以使用File构造函数File(URI uri)URL网址转换为URI,并将其作为File构造函数的参数传递,如下所示:

File file = new File(url.toURI());

关于java - java.io.FileNotFoundException即使文件路径正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62696218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com