如何从Java中的URI获取正确的文件夹句柄? [英] How to get a proper folder handle from a URI in Java?

查看:201
本文介绍了如何从Java中的URI获取正确的文件夹句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到了一个小问题.我的应用程序获取的是文件的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 .: Filejava.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实际上是URIString表示形式.您要调用的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆