将文件名转换为 URL() 后发生 Java IO FileNotFoundException

发布于 12-02 07:11 字数 336 浏览 3 评论 0原文

这是 Jython 代码(尽管这可能不是 Jython 特定的问题)...

file_name = "Manifest.ttl"
file_url = File(file_name).toURL()
f = File(file_url.toString())

java.io.FileNotFoundException:java.io.FileNotFoundException: 文件:/home/james/projects/wordnet/wordnet30/rdf/Manifest.ttl(没有这样的 文件或目录)

Here is the Jython code (although this may not be a Jython-specific issue)...

file_name = "Manifest.ttl"
file_url = File(file_name).toURL()
f = File(file_url.toString())

java.io.FileNotFoundException: java.io.FileNotFoundException:
file:/home/james/projects/wordnet/wordnet30/rdf/Manifest.ttl (No such
file or directory)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去2024-12-09 07:11:26

Javadoc 救援:

通过转换给定的路径名​​字符串创建一个新的 File 实例
到抽象路径名中。如果给定的字符串是空字符串,
那么结果是空的抽象路径名。

参数:
pathname - 路径名字符串

File 构造函数采用抽象路径名作为参数,而不是 URL 的 toString 表示形式。

此外,toURL 已被弃用。您可以使用 toURI,并使用此 URI 重建文件。

Javadoc to the rescue:

Creates a new File instance by converting the given pathname string
into an abstract pathname. If the given string is the empty string,
then the result is the empty abstract pathname.

Parameters:
pathname - A pathname string

The File constructor takes an abstract path name as argument, not the toString representation of a URL.

Besides, toURL is deprecated. You might use toURI, and reconstruct the file with this URI.

围归者2024-12-09 07:11:26

toURL() 添加正确的 URL/URI 所需的 file:// 前缀。显然,File 构造函数不会检查并删除此前缀,因此它会查找名为“file://...”的文件,而不是您希望它查找的文件“/home/james/...”。

toURL() adds the file:// prefix that a proper URL/URI requires. Clearly the File constructor doesn't check and remove this prefix, so it's looking for a file called "file://..." instead of where you want it to look "/home/james/...".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文