java 开发 文件夹创建和删除

//返回文件名称(文件夹读取文件)
public static  ArrayList<String> getFilesPath(String path) throws Exception {
//目标集合fileList
ArrayList<String> fileList = new ArrayList<String>();
File file = new File(path);
if(file.isDirectory()){
File []files = file.listFiles();
for(File fileIndex:files){
//如果这个文件是目录,则进行递归搜索
String str = fileIndex.getPath().replaceAll("\\\\", "/");
fileList.add(fileIndex.getName());
}
}
return fileList;

}


//返回文件路径(文件夹读取文件)
public static  ArrayList<String> getFilesPath1(String path) throws Exception {
//目标集合fileList
ArrayList<String> fileList = new ArrayList<String>();
File file = new File(path);
if(file.isDirectory()){
File []files = file.listFiles();
for(File fileIndex:files){
//如果这个文件是目录,则进行递归搜索
fileList.add(fileIndex.getPath());
}
}
return fileList;

}


/** 
* 删除单个文件 
* @param   sPath    被删除文件的文件名 
* @return 单个文件删除成功返回true,否则返回false 
*/  
public static boolean deleteFile(String sPath) {  
    boolean flag = false;  
    File file = new File(sPath);  
    // 路径为文件且不为空则进行删除  
    if (file.isFile() && file.exists()) {  
        file.delete();  
        flag = true;  
    }  
    return flag;  
}  

猜你喜欢

转载自blog.csdn.net/qq_37996327/article/details/80809834