当前位置: 首页 > news >正文

文件工具类 - FileUtils

@Slf4j
@Component
public class FileUtils {/*** 文件夹复制到指定的文件夹*/@SneakyThrowspublic static void copyDir(File source, File target) {if (!target.exists()) {boolean mkdirs = target.mkdirs();}if (source.isDirectory()) {File[] files = source.listFiles();if (files != null) {for (File file : files) {File inFile = new File(target, file.getName());if (file.isDirectory()) {copyDir(file, inFile);} else {Files.copy(file.toPath(), inFile.toPath(), StandardCopyOption.REPLACE_EXISTING);}}}}}/*** 删除文件或文件夹(包括文件夹下的文件)*/@SneakyThrowspublic static void removeFile(File target) {if (target.exists()) {File[] files = target.listFiles();if (files != null) {for (File file : files) {if (file.isDirectory()) {removeFile(file);} else {boolean delete = target.delete();}}}boolean delete = target.delete();}}/*** 文件复制(如果目标文件存在就会替换)* source 和 target 都是全路径包括文件名(例如:C:\Users\干饭人.pdf)*/@SneakyThrowspublic static void copyFile(File source, File target) {if (!target.exists()) {boolean newFile = target.createNewFile();}try (FileInputStream fileInputStream = new FileInputStream(source);FileOutputStream fileOutputStream = new FileOutputStream(target)) {int len;byte[] bytes = new byte[1024];while ((len = fileInputStream.read(bytes)) != -1) {fileOutputStream.write(bytes, 0, len);}fileOutputStream.flush();} catch (IOException e) {e.printStackTrace();}}/*** 文件夹压缩成 zip 包* @param source 要压缩的文件夹* @param target 压缩后存放路径*/public static String zipDir(File source, File target) {try {if (!target.exists()) {boolean mkdirs = target.mkdirs();}File targetFile = new File(target, source.getName() + ".zip");FileOutputStream fos = new FileOutputStream(targetFile);ZipOutputStream zipOutputStream = new ZipOutputStream(fos);zipSource(source, source.getName(), zipOutputStream);zipOutputStream.close();fos.close();return targetFile.getCanonicalPath();} catch (Exception e) {log.error("压缩文件夹失败:{}", ExceptionUtil.stacktraceToString(e));return "";}}// 将目标文件夹中的一个个文件进行压缩,保留其目录结构private static void zipSource(File source, String name, ZipOutputStream zipOutputStream) throws IOException {if (source.isDirectory()) {// 继续遍历for (File file : Objects.requireNonNull(source.listFiles())) {zipSource(file, name + "/" + file.getName(), zipOutputStream);}} else {FileInputStream fileInputStream = new FileInputStream(source);zipOutputStream.putNextEntry(new ZipEntry(name));int len;byte[] bytes = new byte[4096];while ((len = fileInputStream.read(bytes)) != -1) {zipOutputStream.write(bytes, 0, len);}zipOutputStream.closeEntry();fileInputStream.close();}}public static void main(String[] args) {File source = new File("C:\\Users\\16372\\Documents\\笔记");File target = new File("C:\\Users\\16372");// copyDir(source, target);// removeFile(target);//copyDir(new File("C:\\Users\\16372\\Documents\\干饭人.pdf"),new File("C:\\Users\\16372"));String s = zipDir(source, target);System.out.println(s);}
}
http://www.lryc.cn/news/412346.html

相关文章:

  • Kafka源码剖析-Producer基于内存缓存池分配ByteBuffer
  • 实习十九:学习笔记
  • OrionX:革新GPU资源管理,助力AI开发团队高效运作
  • RabbitMQ发送者重连、发送者确认
  • 请转告HPC计算AI计算单位,选对存储事半功倍
  • [GYCTF2020]Blacklist1
  • Blackcat V2.2付费会员制WordPress资源站主题
  • 动手学强化学习 第 18 章 离线强化学习 训练代码
  • Python笔试面试题AI答之面向对象常考知识点
  • 面试经典算法150题系列-数组/字符串操作之买卖股票最佳时机
  • 安装jdk和tomcat
  • mongodb 备份还原
  • day27——homework
  • shell脚本自动化部署
  • C语言| 文件操作详解(二)
  • 保证项目如期上线,测试人能做些什么?
  • 【杂谈】在大学如何学得计算机知识,浅谈大一经验总结
  • Superset二次开发之柱状图实现同时显示百分比、原始值、汇总值的功能
  • 堆的创建和说明
  • 【玩转python】入门篇day14-函数
  • uni-app 将base64图片转换成临时地址
  • C#用Socket实现TCP客户端
  • jmeter-beanshell学习15-输入日期,计算前后几天的日期
  • Zabbix 7.0 安装
  • 软考高级-系统架构设计师
  • Notepad++ 安装 compare 插件
  • 大数据技术原理-spark的安装
  • 第四范式上线搜广推一体化平台 赋能企业高效增长
  • 智能小程序 Ray 开发面板 SDK —— 智能设备模型通用能力一键执行 SDK 汇总(一)
  • 特大喜讯:我的作品被河北某大学选做教材