FileUtils 文件操作工具类

public class FileUtils {

//用来保存文件的绝对路径的list
//static Vector<String> Filelist = new Vector<String>();

/**
 * 读取文件夹下的所有文件并获取文件的绝对路径
 * @param path
 */ 
public static  Enumeration<String> readFilesDir(String path){
    LinkedList<File> Dirlist = new LinkedList<File>();
    Vector<String> Filelist = new Vector<String>();

    File dir = new File(path);
    File[] files = dir.listFiles(); 

    for(File file : files){
        if(file.isDirectory()){
            Dirlist.add(file);
        }else{
            //处理文件内容
            Filelist.add(file.getAbsolutePath());
            System.out.println(file.getAbsolutePath());
        }
    }

    File temp;
    while(!Dirlist.isEmpty()){
        temp = Dirlist.removeFirst();
        if(temp.isDirectory()){
            files = temp.listFiles();
            if(files == null) continue;
            for(File file : files){
                if(file.isDirectory()){
                    Dirlist.add(file);
                }else{
                    //处理文件内容
                    Filelist.add(file.getAbsolutePath());
                    System.out.println(file.getAbsolutePath());
                }
            }
        }else{
            //处理文件内容,这种情况好像不会发生
            System.out.println("-------------");
        }
    }
	return Filelist.elements();
}


/**
 * 读取单个文件的内容
 * @param file
 * @return 文件的内容,String
 */
public static String readContents(File file){
    StringBuilder res = new StringBuilder();
    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(file));
        while(br.ready()){
            res.append(br.readLine() + "\n");
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return res.toString();
}

// 删除指定文件夹
// param folderPath 文件夹完整绝对路径

public static void delFolder(String folderPath) {
	try {
		delAllFile(folderPath); // 删除完里面所有内容
		String filePath = folderPath;
		filePath = filePath.toString();
		java.io.File myFilePath = new java.io.File(filePath);
		myFilePath.delete(); // 删除空文件夹
	} catch (Exception e) {
		e.printStackTrace();
	}
}

// 删除指定文件夹下所有文件
// param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
	boolean flag = false;
	File file = new File(path);
	if (!file.exists()) {
		return flag;
	}
	if (!file.isDirectory()) {
		return flag;
	}
	String[] tempList = file.list();
	File temp = null;
	for (int i = 0; i < tempList.length; i++) {
		if (path.endsWith(File.separator)) {
			temp = new File(path + tempList[i]);
		} else {
			temp = new File(path + File.separator + tempList[i]);
		}
		if (temp.isFile()) {
			temp.delete();
		}
		if (temp.isDirectory()) {
			delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
			delFolder(path + "/" + tempList[i]);// 再删除空文件夹
			flag = true;
		}
	}
	return flag;
}

 /**
 * 判断文件夹是否存在,如果不存在则新建
 * 
 * @param dirPath 文件夹路径
 */
public static void isFilePathExist(String dirPath) {
    File file = new File(dirPath);
    if (!file.exists()) {
        file.mkdirs();
    }
    
}



public static void main(String[] args) {
	//D:\test\ftp20190726\2
	//D:/test/ftp20190726/test
    String path = "D:\\test\\ftp20190726\\2";
    //String out = "F:/test";
    //readFilesDir(path);
    delAllFile(path);

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章