服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android查看文件夹大小以及删除文件夹的工具类

Android查看文件夹大小以及删除文件夹的工具类

2022-02-27 16:39独在黑夜看湖面 Android

这篇文章主要介绍了Android查看文件夹大小以及删除文件夹的工具类,Android计算文件夹大小和删除目录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在开发中当程序发生ANR或者异常,我们会将信息存在本地,然后上传服务器,这样可以实时去发现问题修改问题。

那我们需要获取文件之后需要对文件进行删除等操作,下面是写的一个工具类,用于查看文件夹大小以及删除文件夹。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import android.text.TextUtils;
 
import java.io.File;
import java.math.BigDecimal;
 
 
public class StorageCleanUtils {
 
 /**
  * 获取文件夹大小(递归)
  *
  * @param file File实例
  * @return long
  */
 public static long getFolderSize(java.io.File file) {
 
  long size = 0;
  try {
   java.io.File[] fileList = file.listFiles();
   for (int i = 0; i < fileList.length; i++) {
    if (fileList[i].isDirectory()) {
     size = size + getFolderSize(fileList[i]);
 
    } else {
     size = size + fileList[i].length();
 
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return size;
 }
 
 
 /**
  * 获取当前文件夹大小,不递归子文件夹
  *
  * @param file
  * @return
  */
 public static long getCurrentFolderSize(File file) {
  long size = 0;
  try {
   java.io.File[] fileList = file.listFiles();
   for (int i = 0; i < fileList.length; i++) {
    if (fileList[i].isDirectory()) {
     //跳过子文件夹
 
    } else {
     size = size + fileList[i].length();
 
    }
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return size;
 }
 
 
 /**
  * 删除指定目录下文件及目录
  *
  * @param deleteThisPath
  * @param filePath
  * @return
  */
 public static boolean deleteFolderFile(String filePath, boolean deleteThisPath) {
  if (!TextUtils.isEmpty(filePath)) {
   try {
    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();
      }
     }
    }
    return true;
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
  }
  return false;
 }
 
 
 /**
  * 删除指定目录下文件
  *
  * @param filePath
  * @return
  */
 public static void deleteFile(String filePath) {
  if (!TextUtils.isEmpty(filePath)) {
   try {
    File file = new File(filePath);
    java.io.File[] fileList = file.listFiles();
    for (int i = 0; i < fileList.length; i++) {
     if (!fileList[i].isDirectory()) {
      fileList[i].delete();
     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
 
 
 /**
  * 格式化单位
  *
  * @param size
  * @return
  */
 public static String getFormatSize(double size) {
  double kiloByte = size / 1024;
  if (kiloByte < 1) {
   return size + "Byte(s)";
  }
 
  double megaByte = kiloByte / 1024;
  if (megaByte < 1) {
   BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
   return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
  }
 
  double gigaByte = megaByte / 1024;
  if (gigaByte < 1) {
   BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
   return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
  }
 
  double teraBytes = gigaByte / 1024;
  if (teraBytes < 1) {
   BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
   return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
  }
  BigDecimal result4 = new BigDecimal(teraBytes);
  return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_32532321/article/details/80522306

延伸 · 阅读

精彩推荐