获取文件夹的子节点名称列表. 文件夹不存在则返回空
实际需求中,可能需要获得传入路径的子节点列表,以便对这些文件进行处理操作
public String[] getDirList(String path, boolean includeSubDirs, boolean includeSubFiles){
if(!isDir(path)){
return null;
}
if(!includeSubDirs && !includeSubFiles){
return null;
}
String[] result = null;
if(includeSubDirs && includeSubFiles){
result = new File(path).list();
}else if(includeSubDirs){
FileFilter fileFilter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.isDirectory();
}
};
File[] files = new File(path).listFiles();
if(files != null && files.length>0){
result = new String[files.length];
for(int i=0; i<files.length; i++){
result[i] = files[i].getName();
}
}
}else{
FileFilter fileFilter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.isDirectory();
}
};
File[] files = new File(path).listFiles();
if(files != null && files.length>0){
result = new String[files.length];
for(int i=0; i<files.length; i++){
result[i] = files[i].getName();
}
}
}
if(result==null || result.length==0)
return null;
else
return result;
}
public boolean isDir(String path){
if(path == null || path .length() <= 0){
return false;
}
boolean result = new File(path).isDirectory();
return result;
}
public boolean isFile(String path){
if(path == null || path.length() <= 0){
return false;
}
boolean result = new File(path).isFile();
return result;
}
以上是具体的代码,可以根据需要对文件夹下的子节点列表进行获得。