Java 为文件生成 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2789241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:52:15  来源:igfitidea点击:

Generate URL for File

java

提问by Dónal

The default output of File.toURL()is

的默认输出File.toURL()

file:/c:/foo/bar

These don't appear to work on windows, and need to be changed to

这些似乎在 Windows 上不起作用,需要更改为

file:///c:/foo/bar

Does the format

有没有格式

file:/foo/bar

work correctly on Unix (I don't have a Unix machine to test on)? Is there a library that can take care of generating a URL from a File that is in the correct format for the current environment?

在 Unix 上正常工作(我没有要测试的 Unix 机器)?是否有一个库可以处理从当前环境正确格式的文件中生成 URL 的库?

I've considered using a regex to fix the problem, something like:

我考虑过使用正则表达式来解决问题,例如:

fileUrl.replaceFirst("^file:/", "file:///")

However, this isn't quite right, because it will convert a correct URL like:

但是,这不太正确,因为它会转换正确的 URL,例如:

file:///c:/foo/bar

to:

到:

file://///c:/foo/bar

Update

更新

I'm using Java 1.4 and in this version File.toURL()is not deprecated and both File.toURL().toString()and File.toURI().toString()generate the same (incorrect) URL on windows

我使用的Java 1.4和在这个版本File.toURL()没有弃用,都File.toURL().toString()File.toURI().toString()产生的窗口相同(不正确)网址

回答by aperkins

The File.toURL() method is deprecated - it is recommended that you use the toURI() method instead. If you use that instead, does your problem go away?

File.toURL() 方法已弃用 - 建议您改用 toURI() 方法。如果你改用它,你的问题会消失吗?



Edit:

编辑:

I understand: you are using Java 4. However, your question did not explain what you were trying to do. If, as you state in the comments, you are attempting to simply read a file, use a FileReaderto do so (or a FileInputStreamif the file is a binary format).

我理解:您使用的是 Java 4。但是,您的问题并没有解释您想要做什么。如果,如您在评论中所述,您只是尝试读取文件,请使用FileReader来执行此操作(如果文件是二进制格式,则使用FileInputStream)。

回答by Jonik

What do you actually mean with "Does the format file:/c:/foo/barwork correctly on Unix"?

“格式file:/c:/foo/bar在 Unix 上是否正常工作”实际上是什么意思?

Some examples from Unix.

一些来自 Unix 的例子。

File file = new File("/tmp/foo.txt"); // this file exists
System.out.println(file.toURI()); // "file:/tmp/foo.txt"

However, you cannot e.g. do this:

但是,您不能这样做:

File file = new File("file:/tmp/foo.txt");
System.out.println(file.exists()); // false

(If you need a URLinstance, do file.toURI().toURL()as the Javadoc says.)

(如果你需要一个URL实例,file.toURI().toURL()按照Javadoc 说的去做。)

Edit: how about the following, does it help?

编辑:以下内容如何,​​有帮助吗?

URL url = new URL("file:/tmp/foo.txt");
System.out.println(url.getFile());  // "/tmp/foo.txt"
File file = new File(url.getFile());
System.out.println(file.exists());  // true

(Basically very close to BalusC's example which used new File(url.toURI()).)

(基本上非常接近 BalusC 使用的示例new File(url.toURI())。)

回答by BalusC

The File(String)expects a pathname, not an URL. If you want to construct a Filebased on a Stringwhich actually represents an URL, then you'll need to convert this Stringback to URLfirst and make use of File(URI)to construct the Filebased on URL#toURI().

File(String)需要一个路径,而不是URL。如果您想File根据String实际表示 URL 的a构造 a ,那么您需要将其转换StringURLfirst 并利用File(URI)来构造File基于 的URL#toURI()

String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.toURI());

Update:since you're on Java 1.4 and URL#toURI()is actually a Java 1.5 method (sorry, overlooked that bit), better use URL#getPath()instead which returns the pathname, so that you can use File(String).

更新:由于您使用的是 Java 1.4 并且URL#toURI()实际上是 Java 1.5 方法(抱歉,忽略了这一点),最好使用URL#getPath()返回路径名的方法,以便您可以使用File(String).

String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.getPath());