温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android java file 清理垃圾获取文件大小 删除文件等操作

发布时间:2020-05-29 08:12:11 来源:网络 阅读:391 作者:龙神号贵 栏目:移动开发

这么久没有写博客了,今天给大家分享一些多file文件的操作。一般可以用到清理垃圾获取文件大小 删除文件等操作,可以直接用于工具类里面,直接做操作便可以

public final class FileUtils {
 public static long fileLen = 0;

 public static void delFilesFromPath(File filePath) {
  if (filePath == null){
   return;
  }
  if (!filePath.exists()){
   return;
  }
  File[] files = filePath.listFiles();
  for (int i = 0; i < files.length; i++) {
   if (files[i].isFile()) {
    files[i].delete();
   } else {
    delFilesFromPath(files[i]);
    files[i].delete();// 刪除文件夾
   }
  }
 }
 //获取文件的大小
 public static String size(File filePath) {
  if (filePath == null){
   return "0字节";
  }
  if (!filePath.exists()){
   return "0字节";
  }
  long fileLen2 = getFileLen(filePath);
  String size = size(fileLen2);
  return size;
 }

 public static long getFileLen(File filePath) {
  fileLen = 0;
  return getFileLenFromPath(filePath);
 }

 public static long getFileLenFromPath(File filePath) {
  File[] files = filePath.listFiles();

  for (int i = 0; i < files.length; i++) {
   if (files[i].isFile()) {
    fileLen += files[i].length();
   } else {
    getFileLenFromPath(files[i]);
   }
  }
  return fileLen;
 }

 private FileUtils() {
 }

 public static String size(long size) {

  if (size / (1024 * 1024 * 1024) > 0) {
   float tmpSize = (float) (size) / (float) (1024 * 1024 * 1024);
   DecimalFormat df = new DecimalFormat("#.##");
   return "" + df.format(tmpSize) + "GB";
  } else if (size / (1024 * 1024) > 0) {
   float tmpSize = (float) (size) / (float) (1024 * 1024);
   DecimalFormat df = new DecimalFormat("#.##");
   return "" + df.format(tmpSize) + "MB";
  } else if (size / 1024 > 0) {
   return "" + (size / (1024)) + "KB";
  } else
   return "" + size + "B";
 }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI