确定资产文件夹中文件的文件类型
问题:如何以编程方式区分资产文件夹中的目录和常规文件?
当使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意你的帖子的评论。但要回答你的问题,请创建一个指向资产目录的 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