/* * Copyright 2016 Freelander * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.jun.elephant.util; import java.io.File; import java.util.Locale; /** * 文件帮助类 * Created by Jun on 2016/9/28. */ public class FileUtils { public static final int BYTE = 1; /** * KB与Byte的倍数 */ public static final int KB = 1024; /** * MB与Byte的倍数 */ public static final int MB = 1048576; /** * GB与Byte的倍数 */ public static final int GB = 1073741824; private FileUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 判断字符串是否为null或全为空格 * * @param s 待校验字符串 * @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格 */ public static boolean isSpace(String s) { return (s == null || s.trim().length() == 0); } /** * 根据文件路径获取文件 * * @param filePath 文件路径 * @return 文件 */ public static File getFileByPath(String filePath) { return isSpace(filePath) ? null : new File(filePath); } /** * 判断文件是否存在 * * @param filePath 文件路径 * @return {@code true}: 存在<br>{@code false}: 不存在 */ public static boolean isFileExists(String filePath) { return isFileExists(getFileByPath(filePath)); } /** * 判断文件是否存在 * * @param file 文件 * @return {@code true}: 存在<br>{@code false}: 不存在 */ public static boolean isFileExists(File file) { return file != null && file.exists(); } /** * 判断是否是目录 * * @param dirPath 目录路径 * @return {@code true}: 是<br>{@code false}: 否 */ public static boolean isDir(String dirPath) { return isDir(getFileByPath(dirPath)); } /** * 判断是否是目录 * * @param file 文件 * @return {@code true}: 是<br>{@code false}: 否 */ public static boolean isDir(File file) { return isFileExists(file) && file.isDirectory(); } /** * 判断是否是文件 * * @param filePath 文件路径 * @return {@code true}: 是<br>{@code false}: 否 */ public static boolean isFile(String filePath) { return isFile(getFileByPath(filePath)); } /** * 判断是否是文件 * * @param file 文件 * @return {@code true}: 是<br>{@code false}: 否 */ public static boolean isFile(File file) { return isFileExists(file) && file.isFile(); } /** * 判断目录是否存在,不存在则判断是否创建成功 * * @param dirPath 文件路径 * @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败 */ public static boolean createOrExistsDir(String dirPath) { return createOrExistsDir(getFileByPath(dirPath)); } /** * 判断目录是否存在,不存在则判断是否创建成功 * * @param file 文件 * @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败 */ public static boolean createOrExistsDir(File file) { // 如果存在,是目录则返回true,是文件则返回false,不存在则返回是否创建成功 return file != null && (file.exists() ? file.isDirectory() : file.mkdirs()); } /** * 获取文件大小 * * @param filePath 文件路径 * @return 文件大小 */ public static String getFileSize(String filePath) { return getFileSize(getFileByPath(filePath)); } /** * 获取文件大小 * <p>例如:getFileSize(file, ConstUtils.MB); 返回文件大小单位为MB</p> * * @param file 文件 * @return 文件大小 */ public static String getFileSize(File file) { if (!isFileExists(file)) return ""; return byte2FitSize(file.length()); } /** * 字节数转合适大小 * <p>保留3位小数</p> * * @param byteNum 字节数 * @return 1...1024 unit */ public static String byte2FitSize(long byteNum) { if (byteNum < 0) { return "shouldn't be less than zero!"; } else if (byteNum < KB) { return String.format(Locale.getDefault(), "%.2fB", (double) byteNum); } else if (byteNum < MB) { return String.format(Locale.getDefault(), "%.2fKB", (double) byteNum / KB); } else if (byteNum < GB) { return String.format(Locale.getDefault(), "%.2fMB", (double) byteNum / MB); } else { return String.format(Locale.getDefault(), "%.2fGB", (double) byteNum / GB); } } /** * 获得某个目录下所有文件大小 * @param dirPath 目录路径 * @return 目录大小(String 类型) */ public static String getCacheDirSize(File file) { return byte2FitSize(getDirSize(file)); } /** * 获取某个目录下所有文件大小 * @param dirPath 目录 * @return 目录大小(long 类型) */ public static long getDirSize(File file) { if (file == null) return 0; if (!isDir(file)) return 0; long dirSize = 0; File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { //文件 dirSize += files[i].length(); } else if (files[i].isDirectory()) {//如果还有文件夹 dirSize += getDirSize(files[i]);//不停回调 } } return dirSize; } /** * 删除目录 * * @param dir 目录 * @return {@code true}: 删除成功<br>{@code false}: 删除失败 */ public static boolean deleteDir(File dir) { if (dir == null) return false; // 目录不存在返回true if (!dir.exists()) return true; // 不是目录返回false if (!dir.isDirectory()) return false; // 现在文件存在且是文件夹 File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { if (!deleteFile(file)) return false; } else if (file.isDirectory()) { if (!deleteDir(file)) return false; } } return dir.delete(); } /** * 删除文件 * * @param srcFilePath 文件路径 * @return {@code true}: 删除成功<br>{@code false}: 删除失败 */ public static boolean deleteFile(String srcFilePath) { return deleteFile(getFileByPath(srcFilePath)); } /** * 删除文件 * * @param file 文件 * @return {@code true}: 删除成功<br>{@code false}: 删除失败 */ public static boolean deleteFile(File file) { return file != null && (!file.exists() || file.isFile() && file.delete()); } }