FileUtil

public class FileUtil {
    public static String formatFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = fileS + " B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + " K";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + " M";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + " G";
        }
        return fileSizeString;
    }
    
    public static String getCacheSize(){
    	String cachePath = DirConstants.DIR_WORK;
    	return formatFileSize(getFolderSize(new File(cachePath)));
    }
    
    public static long getFolderSize(File folder){
    	long size = 0;  
        java.io.File[] fileList = folder.listFiles();  
        for (int i = 0; i < fileList.length; i++)  {  
            if (fileList[i].isDirectory())  {  
                size = size + getFolderSize(fileList[i]);  
            } else  {  
                size = size + fileList[i].length();  
            }  
        }  
        return size; 
    }


    public static String combinPath(String path, String fileName) {
        return path + (path.endsWith(File.separator) ? "" : File.separator)
                + fileName;
    }


    public static void clearCache(){
    	String cachePath = DirConstants.DIR_WORK;
    	try {
			deleteFolderFile(cachePath,false);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public static void deleteFolderFile(String filePath, boolean deleteThisPath)  
            throws IOException {  
        if (!TextUtils.isEmpty(filePath)) {  
            File file = new File(filePath);  
  
            if (file.isDirectory()) {// 处理目录  
                File files[] = file.listFiles();  
                for (int i = 0; i < files.length; i++) {  
                    deleteFolderFile(files[i].getAbsolutePath(), true);  
                }  
            }  
            if (deleteThisPath) {  
                if (!file.isDirectory()) {// 如果是文件,删除  
                    file.delete();  
                } else {// 目录  
                    if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除  
                        file.delete();  
                    }  
                }  
            }  
        }  
    }  
    public static boolean copyFile(File src, File tar) throws Exception {
        if (src.isFile()) {
            InputStream is = new FileInputStream(src);
            OutputStream op = new FileOutputStream(tar);
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(op);
            byte[] bt = new byte[1024 * 8];
            int len = bis.read(bt);
            while (len != -1) {
                bos.write(bt, 0, len);
                len = bis.read(bt);
            }
            bis.close();
            bos.close();
        }
        if (src.isDirectory()) {
            File[] f = src.listFiles();
            tar.mkdir();
            for (int i = 0; i < f.length; i++) {
                copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile()
                        + File.separator + f[i].getName()));
            }
        }
        return true;
    }


    public static boolean moveFile(File src, File tar) throws Exception {
        if (copyFile(src, tar)) {
            deleteFile(src);
            return true;
        }
        return false;
    }


    public static void deleteFile(File f) {
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null && files.length > 0) {
                for (int i = 0; i < files.length; ++i) {
                    deleteFile(files[i]);
                }
            }
        }
        f.delete();
    }


    public static String getMIMEType(String name) {
        String type = "";
        String end = name.substring(name.lastIndexOf(".") + 1, name.length())
                .toLowerCase();
        if (end.equals("apk")) {
            return "application/vnd.android.package-archive";
        } else if (end.equals("mp4") || end.equals("avi") || end.equals("3gp")
                || end.equals("rmvb")) {
            type = "video";
        } else if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
                || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
            type = "audio";
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
                || end.equals("jpeg") || end.equals("bmp")) {
            type = "image";
        } else if (end.equals("txt") || end.equals("log")) {
            type = "text";
        } else {
            type = "*";
        }
        type += "/*";
        return type;
    }


    public static String getExtension(String uri) {
        if (uri == null) {
            return null;
        }


        int dot = uri.lastIndexOf(".");
        if (dot >= 0) {
            return uri.substring(dot);
        } else {
            // No extension.
            return "";
        }
    }


    public static String getPathExceptSDCard(String apkPath) {
        String tmp = "sdcard/";
        int startIndex = apkPath.indexOf(tmp) + tmp.length();
        return apkPath.substring(startIndex);
    }


    public static void mkdirs(String fileDir) {
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }


    public static File createFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }


    public static boolean writeContent(String filePath, String content) {
        try {
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            fos.write(content.getBytes());
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static String readContent(String filePath) {
        try {
            byte[] buffer = new byte[(int) new File(filePath).length()];
            FileInputStream fis = new FileInputStream(new File(filePath));
            fis.read(buffer);
            fis.close();
            return new String(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getSDPath() {
        if (Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            File sdDir = Environment.getExternalStorageDirectory();
            return sdDir.getPath();
        }
        return "/sdcard";
    }


    public static String getFileName(String urlPath) {
        int start = urlPath.lastIndexOf("/");
        if (start != -1) {
            return urlPath.substring(start + 1);
        } else {
            return null;
        }
    }


    public static String getFileNameFromUrl(String url) {
        if (url == null) {
            return null;
        }
        String filename = url.substring(url.lastIndexOf("/") + 1);
        return filename;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值