检查路径是表示文件还是文件夹 [英] Check if a path represents a file or a folder
本文介绍了检查路径是表示文件还是文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个有效的方法来检查 String
是否代表文件或目录的路径。 Android中有效的目录名称是什么?当它出来时,文件夹名称可以包含'。'
字符,那么系统如何理解是存在文件还是文件夹?在此先感谢。
I need a valid method to check if a String
represents a path for file or a directory. What are valid directory names in Android? As it comes out, folder names can contain '.'
chars, so how does system understand whether there's a file or a folder? Thanks in advance.
推荐答案
假设路径
是你的字符串
。
File file = new File(path);
boolean exists = file.exists(); // Check if the file exists
boolean isDirectory = file.isDirectory(); // Check if it's a directory
boolean isFile = file.isFile(); // Check if it's a regular file
参见 文件
Javadoc
See File
Javadoc
或者您可以使用NIO类文件
并检查以下内容:
Or you can use the NIO class Files
and check things like this:
Path file = new File(path).toPath();
boolean exists = Files.exists(file); // Check if the file exists
boolean isDirectory = Files.isDirectory(file); // Check if it's a directory
boolean isFile = Files.isRegularFile(file); // Check if it's a regular file
这篇关于检查路径是表示文件还是文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文