如何从Java中的URI获取正确的文件夹句柄? [英] How to get a proper folder handle from a URI in Java?
问题描述
我注意到了一个小问题.我的应用程序获取的是文件的URI,例如file:///C:/temp/somefolder/somedocument.txt
作为字符串.
I noticed a little issue. What my application gets is a URI of a file e.g. file:///C:/temp/somefolder/somedocument.txt
as a String.
我的应用程序要做的是检查文件夹中是否有更多文件并进行处理.为此,我通常会做类似的事情
What my application has to do is check the folder for more files and process them. To do that, I'd normally do something like
File file = new File(myURI);
File folder = file.getParentFile();
File[] peers = folder.getParentFile().listFiles();
不幸的是,当您使用URI时,这似乎根本不起作用.即使我打开文件夹的URI的File()
句柄,.listFiles
始终返回null.
Unfotunately, this doesn't seem to work at all when you use URI's. .listFiles
always returns null, even if I open a File()
handle to the URI of the folder.
有什么办法解决这个问题吗?
Any ideas how to get around this problem?
P.S .: File
或java.net.URI
的任何方法都不会返回不是URI的绝对路径;)
P.S.: none of the methods of File
or java.net.URI
will return an absolute path which is not a URI ;)
推荐答案
您的myURI
实际上是URI
的String
表示形式.您要调用的File
的构造函数是
Your myURI
is actually a String
representation of a URI
. The constructor of File
that you're calling is
public File(String pathname)
请注意pathname
位,它不是URI值.
Note the pathname
bit, it's not a URI-ish value.
尝试
File file = new File(new URI(myURI));
这篇关于如何从Java中的URI获取正确的文件夹句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!