文件處理工具類

public class FileUtils {

    private static final String TAG = "FileUtils";

    public static final String BASE_PATH = "/storage/sdcard0";

    /**
     * 判斷是否插入SD卡
     * @return
     */
    public static boolean isSDCardMounted() {
        String state = Environment.getStorageState(new File(BASE_PATH));
        File dir = new File(BASE_PATH);
        if (!dir.exists()) {
            return false;
        }
        if (Environment.MEDIA_MOUNTED.equals(state) && dir.isDirectory() && dir.canWrite()) {
            Log.d(TAG, "External storage state=" + state);
            return true;
        }
        return false;
    }
    /**
     * 複製單個文件
     * @param oldPath
     * @param newPath
     */
    public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在時
                InputStream inStream = new FileInputStream(oldPath); //讀入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字節數 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("複製單個文件操作出錯");
            e.printStackTrace();

        }

    }

    /**
     * 複製整個文件夾內容
     * @param oldPath
     * @param newPath
     * @return
     */
    public static boolean copyFolder(String oldPath, String newPath) {
        try {
            File newFile = new File(newPath);
            if (!newFile.exists()) {
                if (!newFile.mkdirs()) {
                    Log.e("--Method--", "copyFolder: cannot create directory.");
                    return false;
                }
            }
            File oldFile = new File(oldPath);
            String[] files = oldFile.list();
            File temp;
            for (String file : files) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file);
                } else {
                    temp = new File(oldPath + File.separator + file);
                }

                if (temp.isDirectory()) {   //如果是子文件夾
                    copyFolder(oldPath + "/" + file, newPath + "/" + file);
                } else if (!temp.exists()) {
                    Log.e("--Method--", "copyFolder:  oldFile not exist.");
                    return false;
                } else if (!temp.isFile()) {
                    Log.e("--Method--", "copyFolder:  oldFile not file.");
                    return false;
                } else if (!temp.canRead()) {
                    Log.e("--Method--", "copyFolder:  oldFile cannot read.");
                    return false;
                } else {
                    FileInputStream fileInputStream = new FileInputStream(temp);
                    FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                    byte[] buffer = new byte[1024];
                    int byteRead;
                    while ((byteRead = fileInputStream.read(buffer)) != -1) {
                        fileOutputStream.write(buffer, 0, byteRead);
                    }
                    fileInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }

            /* 如果不需要打log,可以使用下面的語句
            if (temp.isDirectory()) {   //如果是子文件夾
                copyFolder(oldPath + "/" + file, newPath + "/" + file);
            } else if (temp.exists() && temp.isFile() && temp.canRead()) {
                FileInputStream fileInputStream = new FileInputStream(temp);
                FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                byte[] buffer = new byte[1024];
                int byteRead;
                while ((byteRead = fileInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, byteRead);
                }
                fileInputStream.close();
                fileOutputStream.flush();
                fileOutputStream.close();
            }
            */
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 刪除某個目錄下面所有文件以及本身
     * @param pPath
     */
    public static void deleteDir(final String pPath) {
        File dir = new File(pPath);
        deleteDirWihtFile(dir);
    }

    public static void deleteDirWihtFile(File dir) {
        if (dir == null || !dir.exists() || !dir.isDirectory())
            return;
        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 刪除所有文件
            else if (file.isDirectory())
                deleteDirWihtFile(file); // 遞規的方式刪除文件夾
        }
        dir.delete();// 刪除目錄本身
    }
    /**
     * 獲取文件夾大小
     *
     * @param file File實例
     * @return long
     */
    public static long getFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) size = size + getFolderSize(fileList[i]);
                else size = size + fileList[i].length();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 將文件大小轉換成字節
     * @param fSize
     * @return
     */
    public static String formatFileSize(long fSize){

        DecimalFormat df = new DecimalFormat("#");

        String fileSizeString = "";

        fileSizeString = df.format((double) fSize/1048576 );


        return fileSizeString;

    }

    /**
     * 判斷文件是否存在
     * @param strFile
     * @return
     */
    public static boolean fileIsExists(String strFile)
    {
        try
        {
            File f=new File(strFile);
            if(!f.exists())
            {
                return false;
            }

        }
        catch (Exception e)
        {
            return false;
        }

        return true;
    }

    /**
     * 刪除單個文件
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            return file.delete();
        }
        return false;
    }

    /**
     * 刪除空的的文件夾
     * @param tempFile
     */
    public static void deleteDirectory(File tempFile) {
        try {
            if(!tempFile.exists()) return;
            if(tempFile.isDirectory()){
                File[] files = tempFile.listFiles();
                if(files == null || files.length == 0) {
                    tempFile.delete();
                    return;
                }
                for (File file: files){
                    if(file.isFile()){
                        file.delete();
                    } else if(file.isDirectory()){
                        deleteDirectory(file);
                    }
                }
                tempFile.delete();
            }else {
                tempFile.delete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章