Android Open Source - CommonLibs File Utils






From Project

Back to project page CommonLibs.

License

The source code is released under:

Apache License

If you think the Android project CommonLibs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.alex.common.utils;
/*from  w  w w.  j av  a2s  .c  o m*/
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.alex.common.AppConfig;

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

/**
 * ???????
 * @author caisenchuan
 */
public class FileUtils {

    /*--------------------------
     * ???
     *-------------------------*/
    private static final String TAG = FileUtils.class.getSimpleName();
    
    /*--------------------------
     * ?????
     *-------------------------*/
    /**
     * ??????
     */
    public enum PathType {
        /**photo??*/
        PHOTO,
        /**download??*/
        DOWNLOAD,
        /**????????*/
        SHARE
    }

    /*--------------------------
     * ????????
     *-------------------------*/

    /*--------------------------
     * public??
     *-------------------------*/
    /**
     * ??????????????
     */
    public static void deleteAllFiles(String path) {
        if(!TextUtils.isEmpty(path)) {
            File f = new File(path);
            deleteAllFiles(f);
        }
    }
    
    /**
     * ?????????????
     * @param root
     */
    public static void deleteAllFiles(File root) {
        File files[] = root.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) { // ??????????
                    deleteAllFiles(f);
                    try {
                        f.delete();
                    } catch (Exception e) {
                        KLog.e(TAG, "Exception", e);
                    }
                } else {
                    if (f.exists()) { // ????????
                        deleteAllFiles(f);
                        try {
                            f.delete();
                        } catch (Exception e) {
                            KLog.e(TAG, "Exception", e);
                        }
                    }
                }
            }
        }
    }
    
    /**
     * ??????
     */
    public static void deleteFile(String picPath) {
        if(picPath != null) {
            File file = new File(picPath);
            if(file != null && file.exists()) {
                file.delete();
            }
            picPath = null;
        }
    }

    /**
     * ??SD?????????
     */
    public static boolean isSDMount() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ????????
     * @param type ??????
     */
    public static String getDir(PathType type) {
        String dir = "";
        if(type == PathType.PHOTO) {
            dir = AppConfig.getDirPhoto();
        } else if(type == PathType.DOWNLOAD) {
            dir = AppConfig.getDirDownload();
        } else if(type == PathType.SHARE) {
          dir = AppConfig.getDirShare();
      } else {
            dir = AppConfig.getDirApp();
        }

        File sd = Environment.getExternalStorageDirectory();
        File file = new File(sd.getPath() + dir);
        if (!file.exists()) {
            file.mkdirs();
        }
    
        return file.getAbsolutePath();
    }

    /**
     * ??????????????? ?? + ??????? ????????
     * @param type ??????
     * @param ext ?????????
     */
    public static String getUniqPath(PathType type, String ext) {
        String picDir = getDir(type);
    
        GregorianCalendar calendar = new GregorianCalendar();
        String date_s = String.format("%d%02d%02d_%02d%02d%02d",
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 
                calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR), 
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
        
        String filename = String.format("%s/%s%s", picDir, date_s, ext);
        File file = new File(filename);
        int i = 1;
    
        // ???????????????
        while (file.exists() && i < 10000) {
            filename = String.format("%s/%s(%s)%s", picDir, date_s, i, ext);
            file = new File(filename);
            i++;
        }
    
        KLog.d(TAG, "filename : " + filename);
    
        return filename;
    }
    
    /*--------------------------
     * protected??packet??
     *-------------------------*/

    /*--------------------------
     * private??
     *-------------------------*/

}




Java Source Code List

com.alex.common.AppConfig.java
com.alex.common.AppControl.java
com.alex.common.Err.java
com.alex.common.OnHttpRequestReturnListener.java
com.alex.common.activities.BaseActivity.java
com.alex.common.activities.ImageLoadActivity.java
com.alex.common.activities.WebViewActivity.java
com.alex.common.apis.HttpApi.java
com.alex.common.exception.RetErrorException.java
com.alex.common.utils.BackgroundHandler.java
com.alex.common.utils.BaiduMapUtils.java
com.alex.common.utils.DateTimeUtils.java
com.alex.common.utils.DeviceUtils.java
com.alex.common.utils.DialogUtils.java
com.alex.common.utils.FileUtils.java
com.alex.common.utils.ImageUtils.java
com.alex.common.utils.KLog.java
com.alex.common.utils.Misc.java
com.alex.common.utils.NetworkUtils.java
com.alex.common.utils.PrefUtils.java
com.alex.common.utils.ShareUtils.java
com.alex.common.utils.StringUtils.java
com.alex.common.utils.ThreadUtils.java
com.alex.common.utils.ToastUtils.java
com.alex.common.views.ZoomImageView.java