确定资产文件夹中文件的文件类型

发布于 2024-09-04 14:46:58 字数 371 浏览 5 评论 0原文

问题:如何以编程方式区分资产文件夹中的目录和常规文件?

当使用AssetManager访问assets文件夹中的文件时,似乎无法确定文件实际上是文件还是目录。您可以从 list 方法获取文件列表,然后使用 open 方法打开该文件。我认为也许使用 openFd 方法来获取资产文件描述符(然后随后请求普通文件描述符)将为我提供一些信息。但是请求目录的文件描述符会导致 IOException (这是有道理的,因为目录拥有文件描述符意味着什么......?)。

目前,我依靠 IOException (由于尝试打开资产文件夹中的目录而产生)来确定文件是否实际上是目录。 (打开常规文件就可以了)。这似乎是个坏主意。还有其他区分文件和目录的建议吗?

Question: How do you programmatically distinguish between directories and regular files in the assets folder?

When using AssetManager to access files in the assets folder, it seems impossible to determine if a file is in fact a file or a directory. You get the list of files from the list method and then open the file using the open method. I thought perhaps using the openFd method to get the asset file descriptor (and then subsequently requesting the normal file descriptor) would provide me some information. But requesting the file descriptor for a directory results in an IOException (which makes sense since what would it mean for a directory to have a file descriptor...?).

Currently I'm relying on that IOException (resulting from attemptng to open a directory in the assets folder) in order to determine if a file is in fact a directory. (Opening a regular file works just fine). This seems like a bad idea. Any other suggestions to distinguish between a file and a directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-09-11 14:46:58

我同意你的帖子的评论。但要回答你的问题,请创建一个指向资产目录的 File 对象
File root = new File("path/to/directory");

然后你可以像这样提取它的所有子文件和目录
File[] files = root.listFiles();

获得列表后,您可以使用 File.isDirectory() 确定哪些内容。

至于确定文件类型,就像使用 String.subString() 获取最后一个句点后面的字符一样简单。

希望这有帮助

I agree with the comments on your post. But to answer your question, make a File object pointed at the assets directory
File root = new File("path/to/directory");

Then you can extract all its sub files and directories like this
File[] files = root.listFiles();

Once you have the list you can determine what is what using File.isDirectory().

As for determining file types, that is as simple as using String.subString() to grab the character's following the last period.

hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文