xUtils之FileUtil
来源:互联网 发布:安防网络工程公司 编辑:程序博客网 时间:2024/06/14 21:32
功能强大的FileUtil
public class FileUtil { private FileUtil() { } public static File getCacheDir(String dirName) { File result; if (existsSdcard()) { File cacheDir = x.app().getExternalCacheDir(); if (cacheDir == null) { result = new File(Environment.getExternalStorageDirectory(), "Android/data/" + x.app().getPackageName() + "/cache/" + dirName); } else { result = new File(cacheDir, dirName); } } else { result = new File(x.app().getCacheDir(), dirName); } if (result.exists() || result.mkdirs()) { return result; } else { return null; } } /** * 检查磁盘空间是否大于10mb * * @return true 大于 */ public static boolean isDiskAvailable() { long size = getDiskAvailableSize(); return size > 10 * 1024 * 1024; // > 10bm } /** * 获取磁盘可用空间 * * @return byte 单位 kb */ public static long getDiskAvailableSize() { if (!existsSdcard()) return 0; File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径 StatFs stat = new StatFs(path.getAbsolutePath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; // (availableBlocks * blockSize)/1024 KIB 单位 // (availableBlocks * blockSize)/1024 /1024 MIB单位 } public static Boolean existsSdcard() { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); } public static long getFileOrDirSize(File file) { if (!file.exists()) return 0; if (!file.isDirectory()) return file.length(); long length = 0; File[] list = file.listFiles(); if (list != null) { // 文件夹被删除时, 子文件正在被写入, 文件属性异常返回null. for (File item : list) { length += getFileOrDirSize(item); } } return length; } /** * 复制文件到指定文件 * * @param fromPath 源文件 * @param toPath 复制到的文件 * @return true 成功,false 失败 */ public static boolean copy(String fromPath, String toPath) { boolean result = false; File from = new File(fromPath); if (!from.exists()) { return result; } File toFile = new File(toPath); IOUtil.deleteFileOrDir(toFile); File toDir = toFile.getParentFile(); if (toDir.exists() || toDir.mkdirs()) { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(from); out = new FileOutputStream(toFile); IOUtil.copy(in, out); result = true; } catch (Throwable ex) { LogUtil.d(ex.getMessage(), ex); result = false; } finally { IOUtil.closeQuietly(in); IOUtil.closeQuietly(out); } } return result; }}
0 0
- xUtils之FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- FileUtil
- fileUtil
- [leetcode] 232. Implement Queue using Stacks
- IntelliJ IDEA启动自动进入最后一个项目
- 哈夫曼树及二叉树
- 笑谈Android图表------MPAndroidChart
- HTTP 301跳转与302跳转的区别
- xUtils之FileUtil
- Java IO流使用,socket基础
- Echarts使用及动态加载图表数据 折线图X轴数据动态加载
- 我从编程总结的 22 个经验
- Storage Cluster Quick Start【存储集群快速入门】
- Android Studio优化技巧
- Multimodal Deep Learning(多模态深度学习)未完待续
- 时间与时间戳的相互转换
- Array与ArrayList的区别