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);

}

}

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