文件工具类的实用方法记录
来源:互联网 发布:数据库default怎么用 编辑:程序博客网 时间:2024/06/05 04:16
如题
不定期更新。
package file;import java.io.*;import java.math.BigDecimal;import java.math.BigInteger;import java.security.MessageDigest;/** * Created on 2017/10/19 * Author: youxingyang. */public final class FileUtils { private static final int BUFFER_SIZE = 16 * 1024; private static final int MD5_RABIX = 16; /** * 统计文件夹下的文件总数 * @param path 文件的绝对路径 * @return */ public static long getNumFromFile(String path) { long res = 0; File file = new File(path); if (file.exists()) { File[] files = file.listFiles(); if (files != null) { for (File file1 : files) { if (file1.isDirectory()) { res += getNumFromFile(file1.getAbsolutePath()); } else { res++; } } } else { if (file.isFile()) { res++; } } } return res; } /** * 获取文件夹下的所有文件的大小 * @param path 文件夹绝对路径 * @return */ public static long getAllSizeFromFile(String path) { long res = 0; File file = new File(path); if (file.exists()) { File[] files = file.listFiles(); if (files != null) { for (File file1 : files) { if (file1.isDirectory()) { res += getAllSizeFromFile(file1.getAbsolutePath()); } else { res += file1.length(); } } } else { if (file.isFile()) { res = file.length(); } } } return res; } /** * 以适当方式显示文件大小. * @param size 单位字节 * @return */ public static String getFormatSize(long size) { long kiloByte = size / 1024; if (kiloByte < 1) { return size + "Byte(s)"; } long megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB"; } long gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB"; } long 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"; } /** * 创建一个目录 * @param filePath * @return */ public static boolean createDir(String filePath) { File file = new File(filePath); // 判断目录是否存在 if (file.isDirectory()) { System.out.println("目标目录已存在" + filePath); return true; } // 判断文件是否为目录 if (filePath.endsWith(File.separator)) { System.out.println("目标文件不能为目录!"); return true; } // 判断目标文件所在的目录是否存在 if (!file.getParentFile().exists()) { // 如果目标文件所在的文件夹不存在,则创建父文件夹 System.out.println("目标文件所在目录不存在,准备创建它!"); // 判断创建目录是否成功 if (!file.getParentFile().mkdirs()) { System.out.println("创建目标文件所在的目录失败!"); return false; } else { return true; } } else { return true; } } /** * 创建一个文件 * @param filePath * @return */ public static boolean createFile(String filePath) { File file = new File(filePath); // 判断文件是否存在 if (file.exists()) { System.out.println("目标文件已存在" + filePath); return false; } // 判断文件是否为目录 if (filePath.endsWith(File.separator)) { System.out.println("目标文件不能为目录!"); return false; } // 判断目标文件所在的目录是否存在 if (!file.getParentFile().exists()) { // 如果目标文件所在的文件夹不存在,则创建父文件夹 System.out.println("目标文件所在目录不存在,准备创建它!"); // 判断创建目录是否成功 if (!file.getParentFile().mkdirs()) { System.out.println("创建目标文件所在的目录失败!"); return false; } } try { // 创建目标文件 if (file.createNewFile()) { System.out.println("创建文件成功:" + filePath); return true; } else { System.out.println("创建文件失败!"); return false; } // 捕获异常 } catch (IOException e) { e.printStackTrace(); System.out.println("创建文件失败!" + e.getMessage()); return false; } } /** * 判断该文件夹下至少有一个文件夹 * @param files * @return */ public static boolean containDir(File[] files) { boolean res = false; for (File file : files) { if (file.isDirectory()) { res = true; break; } } return res; } /** * 获得md5 * @param file 文件 * @return * @throws FileNotFoundException */ public static String getFileMd5(File file) throws FileNotFoundException { FileInputStream in = new FileInputStream(file); return md5(in); } /** * 获得md5 * @param fileName 文件名 * @return * @throws FileNotFoundException */ public static String getFileMd5(String fileName) throws FileNotFoundException { return getFileMd5(new File(fileName)); } /** * 获得md5 * @param inputStream 流 * @return */ public static String md5(InputStream inputStream) { String value = null; try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] byteBuffer = new byte[BUFFER_SIZE]; int readLength = inputStream.read(byteBuffer); while (readLength != -1) { md5.update(byteBuffer, 0, readLength); readLength = inputStream.read(byteBuffer); } BigInteger bi = new BigInteger(1, md5.digest()); value = bi.toString(MD5_RABIX); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return value; } public static void main(String[] args) { System.out.println(getFormatSize(getAllSizeFromFile("E:\\3提交站"))); }}
阅读全文
0 0
- 文件工具类的实用方法记录
- 记录log日志文件的工具类
- android中,非常实用的 类,方法,工具
- 一些实用的工具方法整合
- java基础—实用的工具方法
- 实用的Http工具类
- 实用的JSON工具类
- 实用的工具类Logutil
- 记录一个非常实用的gif制作工具licecap
- java工具类方法记录
- Arduino库文件LiquidCrystal的实用方法
- 实用的网站记录
- 实用的网站记录
- org.apache.commons.io---实用的文件操作工具
- Linux下实用的文件编码转换命令工具iconv
- 实用的本机文件搜索工具 Everything Search Engine
- 《程序员》 -- 学习新工具新技术的两种实用方法
- underscore.js 源码中一些实用的工具方法
- 第970期机器学习日报(2017-05-15)
- 第968期机器学习日报(2017-05-13)
- PAT 1013. 数素数 (20)
- 第967期机器学习日报(2017-05-12)
- 第971期机器学习日报(2017-05-16)
- 文件工具类的实用方法记录
- PHP 方法
- 面向对象总结
- 第973期机器学习日报(2017-05-18)
- 第七周 顺序环形队列
- python 学习笔记 第一篇:开发环境搭建
- 第5周项目2——建立链栈算法库
- 第五周-项目2
- MFC中增加控制台黑白框方法