android 파일 작업 도구 클래스

19520 단어 자바android
package com.baize.love.utils;

import android.os.Environment;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *      
 * Created by lxb on 2017/4/11.
 */

public class FileUtils {

    public static Map,List> allFilesDict = null;

    /**
     *     
     *
     * @param path
     * @return
     */
    public static boolean deleteFile(String path) {
        if (isBlank(path)) {
            return true;
        }

        File file = new File(path);
        return deleteFile(file);
    }

    /**
     *     
     *
     * @param file
     * @return
     */
    public static boolean deleteFile(File file) {

        if (!file.exists()) {
            return true;
        }
        if (file.isFile()) {
            return file.delete();
        }
        if (!file.isDirectory()) {
            return false;
        }
        for (File f : file.listFiles()) {
            if (f.isFile()) {
                f.delete();
            } else if (f.isDirectory()) {
                deleteFile(f.getAbsolutePath()); //  
            }
        }
        return file.delete();

    }


    public static boolean isBlank(String str) {

        return (str == null || str.trim().length() == 0);
    }

    /**
     *               
     *
     * @param path
     * @return
     */
    public static boolean isExit(String path) {
        File file = null;
        try {
            file = new File(path);
            return file.exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     *                   
     * @param dir     
     * @return
     */
    public static Map,List> getAllFilePath(String dir){


        File tmpFileObj = new File(dir);

        if(tmpFileObj.exists()){

            File[] files = tmpFileObj.listFiles();

            if (files.length == 0) {

                BZLogUtils.getInstance().print("      !");

                return null;

            } else {
                String key = null;
                List tmpFileList = null;

                for (File file2 : files) {

                    if (file2.isDirectory()) {

                        tmpFileList = new ArrayList<>();

                        String fileDir = file2.getAbsolutePath();

                        key = fileDir.substring(fileDir.lastIndexOf("/") + 1,fileDir.length());

                        BZLogUtils.getInstance().print("   :" + file2.getAbsolutePath() + "  key: "+key);

                        getAllFilePath(file2.getAbsolutePath());        //            


                    } else {
                        String fileDir = file2.getParent();

                        key = fileDir.substring(fileDir.lastIndexOf("/") + 1,fileDir.length());

                        if(tmpFileList == null){
                            tmpFileList = new ArrayList<>();
                        }

                        tmpFileList.add(file2.getAbsolutePath());

                        getAllFileDict().put(key,tmpFileList);

                        BZLogUtils.getInstance().print("  :" + file2.getAbsolutePath() + "   :" + key);

                    }
                }
                BZLogUtils.getInstance().print("size:"+getAllFileDict().size());
            }
        } else {

            BZLogUtils.getInstance().print("     !");
        }

        return getAllFileDict();

    }

    /**
     *         
     *
     * @return
     */
    public static Map,List> getAllFileDict(){

        if(allFilesDict == null){
            allFilesDict = new HashMap<>();
        }
        return allFilesDict;

    }

    public static void recycRes(){
        allFilesDict = null;

    }

    /**
     *        
     *
     * @param dir   
     * @param fileName    
     * @return
     */
    public static String createBuildPath(String dir,String fileName) {

        return Environment.getExternalStorageDirectory() + "/BZLove/" + dir + "/" + fileName;
    }

    /**
     *     
     * @param dir
     * @return
     */
    public static String createDir(String dir){
        String tmpDir = Environment.getExternalStorageDirectory() + "/BZLove/" + dir + "/";
        File file = new File(tmpDir);
        if(!file.exists()){
            if(file.mkdirs()){
                return tmpDir;
            }
            BZLogUtils.getInstance().print("      ");
            return null;
        }
        return tmpDir;
    }

    /**
     *            
     * @return
     */
    public static String getBuildRootDir(){

        String tmpDir = Environment.getExternalStorageDirectory() + "/BZLove/";

        File file = new File(tmpDir);

        if(!file.exists()){

            if(file.mkdirs()){
                return tmpDir;
            }
            BZLogUtils.getInstance().print("      ");
            return null;
        }
        return null;
    }
}

좋은 웹페이지 즐겨찾기