Java 本地资源的 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2466369/
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
URI for local resources
提问by Anto
In Java can I create a URI for a file located locally in the hard drive? If so, how should it be constructed?
在 Java 中,我可以为位于硬盘驱动器本地的文件创建 URI 吗?如果可以,应该如何构建?
采纳答案by Roman
Look at File documentation:
查看文件文档:
File has a constructor, which takes URI as a parameter, and it also has method toURI()
if you want to get URI from existing file. You can play with that to understand how the things should be done.
File 有一个构造函数,它以 URI 作为参数,toURI()
如果你想从现有文件中获取 URI ,它也有方法。您可以使用它来了解应该如何完成这些事情。
You can also read about URI(it's not a java term). There is an example in Wikipedia:
您还可以阅读有关URI(它不是 Java 术语)的信息。维基百科中有一个例子:
file:///home/username/RomeoAndJuliet.pdf
file:///home/username/RomeoAndJuliet.pdf
回答by Taylor Leese
Try below:
试试下面:
URI uri = new URI("file:///C:/other/mydir/myfile.txt");
回答by Roman
URI uri = new URI("file:///filename.txt");
If you're using Windows:
如果您使用的是 Windows:
URI uri = new URI("file:///C:/fun/filename.txt");