检查路径是否代表文件或文件夹 [英] 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?
推荐答案
假设 path
是你的 String
.
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
参见 File
Javadoc
See File
Javadoc
或者您可以使用 NIO 类 Files
并检查如下内容:
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屋!
查看全文