Android file tool class

1. File tool class

package com.android.myapplication;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;


public class FileUtils {

    /**
     * Create root cache directory
     *
     * @return
     */
    public static String createRootPath(Context context) {
        String cacheRootPath = "";
        if (isSdCardAvailable()) {
            // /sdcard/Android/data/<application package>/cache
            cacheRootPath = context.getExternalCacheDir().getPath();
        } else {
            // /data/data/<application package>/cache
            cacheRootPath = context.getCacheDir().getPath();
        }
        return cacheRootPath;
    }

    public static boolean isSdCardAvailable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }


    /**
     * recursively create files
     * @param file
     * @return Creation failure returns ""
     */
    public static String createFile(File file) {
        try {
            if (file.getParentFile().exists()) {
                Log.i("==","-----Create file" + file.getAbsolutePath());
                file.createNewFile();
                return file.getAbsolutePath();
            } else {
                createDir(file.getParentFile().getAbsolutePath());
                file.createNewFile();
                Log.i("==","-----Create file" + file.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return "";
    }

    /**
     * recursively create folders
     * @param dirPath
     * @return Creation failure returns ""
     */
    public static String createDir(String dirPath) {
        try {
            File file = new File(dirPath);
            if (file.getParentFile().exists()) {
                Log.i("==","-----Create folder" + file.getAbsolutePath());
                file.mkdir();
                return file.getAbsolutePath();
            } else {
                createDir(file.getParentFile().getAbsolutePath());
                Log.i("==","-----Create folder" + file.getAbsolutePath());
                file.mkdir();
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return dirPath;
    }


    /**
     * Convert file size
     * @param fileLen unit B
     * @return
     */
    public static String formatFileSizeToString(long fileLen) {
        DecimalFormat df = new DecimalFormat("0.00");
        String fileSizeString = "";
        if (fileLen < 1024) {
            fileSizeString = df.format((double) fileLen) + "B";
        } else if (fileLen < 1048576) {
            fileSizeString = df.format((double) fileLen / 1024) + "K";
        } else if (fileLen < 1073741824) {
            fileSizeString = df.format((double) fileLen / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) fileLen / 1073741824) + "G";
        }
        return fileSizeString;
    }
    
    /**
     * file copy
     * @param src source file
     * @param desc destination file
     */
    public static void fileChannelCopy(File src, File desc) {
        //createFile(src);
        createFile(desc);
        FileInputStream fi = null;
        FileOutputStream fo = null;
        try {
            fi = new FileInputStream(src);
            fo = new FileOutputStream(desc);
            FileChannel in = fi.getChannel();//Get the corresponding file channel
            FileChannel out = fo.getChannel();//Get the corresponding file channel
            in.transferTo(0, in.size(), out);//Connect two channels, and read from the in channel, then write to the out channel
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            try {
                if (fo != null) fo.close();
                if (fi != null) fi.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }


}
Reprinted from: https://github.com/yangchong211/LifeHelper/blob/master/app/src/main/java/com/ns/yc/lifehelper/utils/FileUtils.java

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894702&siteId=291194637