In Java can I create a URI for a file located locally in the hard drive? If so, how should it be constructed?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
You can also read about URI (it's not a java term). There is an example in Wikipedia:
file:///home/username/RomeoAndJuliet.pdf
回答2:
URI uri = new URI("file:///filename.txt");
If you're using Windows:
URI uri = new URI("file:///C:/fun/filename.txt");
回答3:
Try below:
URI uri = new URI("file:///C:/other/mydir/myfile.txt");