package cn.tee3.manager.util;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class FileManager {

    private static String TAG = "FileManager";

    public static boolean fileCheck(String dir, boolean isAbsPath, boolean isRebuild) {
        boolean isFileExists = true;

        if (!isAbsPath) {
            if (Environment.isExternalStorageEmulated()) {
                dir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + dir;
            }
        }

        try {
            File file = new File(dir);
            if (isRebuild && file.exists()) {
                file.delete();
            }
            if (!file.exists()) {
                isFileExists = false;
                if (!file.getParentFile().exists()) {
                    boolean mkdirs = file.getParentFile().mkdirs();
                    if (!mkdirs) {
                        Log.w(TAG, "创建目录失败！");
                    }
                }
                boolean newFile = file.createNewFile();
                if (!newFile) {
                    Log.w(TAG, "创建文件失败！");
                }
            } else if (isRebuild) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return isFileExists;
    }
}
