FileUtils.java 1.96 KB
package com.polysoft.nafmii.utils;

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

import java.io.File;

public class FileUtils {

    public static File getIndex() {
        return WebVersionUtils.getIndex();
    }

    public static File getDistDir() {
        return WebVersionUtils.getDistDir();
    }

    public static File getDownloadDir() {
        File download = new File(getRootDir(), "download");
        if (!download.exists()) {
            download.mkdirs();
        }
        return download;
    }

    public static File getSysDownLoadDir() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
//        return CtxUtils.getApplication().getExternalFilesDir();
    }

    public static File getRootDir() {
        return CtxUtils.getApplication().getFilesDir();
    }

    public static File getRootCacheDir() {
        return CtxUtils.getApplication().getCacheDir();
    }

    public static void deletes(File file) {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                deletes(f);
            }
        }
        file.delete();
    }

    public static String getFileName(String name) {
        int nameMaxLength = 60;
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > nameMaxLength) {
                String newname = name.substring(0, nameMaxLength);
                String[] split = name.split("\\.");
                if (split.length > 1) {
                    return newname + "." + split[split.length - 1];
                }
                return newname;
            }
            return name;
        }
        return "";
    }

    public static String getUrlFileName(String url) {
        if (!TextUtils.isEmpty(url)) {
            String[] split = url.split("/");
            if (split.length > 1) {
                String name = split[split.length - 1];
                return getFileName(name);
            }
        }
        return "";
    }
}