FileUtil 文件操作工具类,7步搞懂分布式全内容

这篇文章介绍了Java中的文件操作方法,如复制、删除、重命名、解压缩和压缩文件,以及提供了一份针对初级到中级Java工程师的学习资源列表,包括面经、学习笔记、实战项目和视频教程,旨在帮助提升技能并解决自学难题。
摘要由CSDN通过智能技术生成
  • @param source source path must not be null

  • @param target target path must not be null

*/

public static void copyFolder(@NonNull Path source, @NonNull Path target) throws IOException {

Assert.notNull(source, “Source path must not be null”);

Assert.notNull(target, “Target path must not be null”);

Files.walkFileTree(source, new SimpleFileVisitor () {

@Override

public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)

throws IOException {

Path current = target.resolve(source.relativize(dir).toString());

Files.createDirectories(current);

return FileVisitResult.CONTINUE;

}

@Override

public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

throws IOException {

Files.copy(file, target.resolve(source.relativize(file).toString()),

StandardCopyOption.REPLACE_EXISTING);

return FileVisitResult.CONTINUE;

}

});

}

/**

  • Deletes folder recursively.

  • @param deletingPath deleting path must not be null

*/

public static void deleteFolder(@NonNull Path deletingPath) throws IOException {

Assert.notNull(deletingPath, “Deleting path must not be null”);

if (Files.notExists(deletingPath)) {

return;

}

log.info(“Deleting [{}]”, deletingPath);

delete(deletingPath.toFile());

log.info(“Deleted [{}] successfully”, deletingPath);

}

public static void delete(File file) {

if(file.isDirectory()){

Arrays.asList(file.listFiles()).forEach(e->{

delete(e);

});

}

file.delete();

}

/**

  • Renames file or folder.

  • @param pathToRename file path to rename must not be null

  • @param newName new name must not be null

*/

public static void rename(@NonNull Path pathToRename, @NonNull String newName)

throws IOException {

Assert.notNull(pathToRename, “File path to rename must not be null”);

Assert.notNull(newName, “New name must not be null”);

Path newPath = pathToRename.resolveSibling(newName);

log.info(“Rename [{}] to [{}]”, pathToRename, newPath);

Files.move(pathToRename, newPath);

log.info(“Rename [{}] successfully”, pathToRename);

}

/**

  • Unzips content to the target path.

  • @param zis zip input stream must not be null

  • @param targetPath target path must not be null and not empty

  • @throws IOException throws when failed to access file to be unzipped

*/

public static void unzip(@NonNull ZipInputStream zis, @NonNull Path targetPath)

throws IOException {

// 1. unzip file to folder

// 2. return the folder path

Assert.notNull(zis, “Zip input stream must not be null”);

Assert.notNull(targetPath, “Target path must not be null”);

// Create path if absent

createIfAbsent(targetPath);

// Folder must be empty

ensureEmpty(targetPath);

ZipEntry zipEntry = zis.getNextEntry();

while (zipEntry != null) {

// Resolve the entry path

Path entryPath = targetPath.resolve(zipEntry.getName());

// Check directory

checkDirectoryTraversal(targetPath, entryPath);

if (zipEntry.isDirectory()) {

// Create directories

Files.createDirectories(entryPath);

} else {

// Copy file

Files.copy(zis, entryPath);

}

zipEntry = zis.getNextEntry();

}

}

/**

  • Unzips content to the target path.

  • @param bytes zip bytes array must not be null

  • @param targetPath target path must not be null and not empty

  • @throws IOException io exception

*/

public static void unzip(@NonNull byte[] bytes, @NonNull Path targetPath) throws IOException {

Assert.notNull(bytes, “Zip bytes must not be null”);

ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bytes));

unzip(zis, targetPath);

}

/**

  • Zips folder or file.

  • @param pathToZip file path to zip must not be null

  • @param pathOfArchive zip file path to archive must not be null

  • @throws IOException throws when failed to access file to be zipped

*/

public static void zip(@NonNull Path pathToZip, @NonNull Path pathOfArchive)

throws IOException {

try (OutputStream outputStream = Files.newOutputStream(pathOfArchive)) {

try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {

zip(pathToZip, zipOut);

}

}

}

/**

  • Zips folder or file.

  • @param pathToZip file path to zip must not be null

  • @param zipOut zip output stream must not be null

  • @throws IOException throws when failed to access file to be zipped

*/

public static void zip(@NonNull Path pathToZip, @NonNull ZipOutputStream zipOut)

throws IOException {

// Zip file

zip(pathToZip, pathToZip.getFileName().toString(), zipOut);

}

/**

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

最后希望可以帮助到大家!

千千万万要记得:多刷题!!多刷题!!

之前算法是我的硬伤,后面硬啃了好长一段时间才补回来,算法才是程序员的灵魂!!!!

篇幅有限,以下只能截图分享部分的资源!!

(1)多线程(这里以多线程为代表,其实整理了一本JAVA核心架构笔记集)

image

(2)刷的算法题(还有左神的算法笔记)

image

(3)面经+真题解析+对应的相关笔记(很全面)

image

(4)视频学习(部分)

ps:当你觉得学不进或者累了的时候,视频是个不错的选择

在这里,最后只一句话:祝大家offer拿到手软!!

法是我的硬伤,后面硬啃了好长一段时间才补回来,算法才是程序员的灵魂!!!!

篇幅有限,以下只能截图分享部分的资源!!

(1)多线程(这里以多线程为代表,其实整理了一本JAVA核心架构笔记集)

[外链图片转存中…(img-JRWBm6tC-1711133160106)]

(2)刷的算法题(还有左神的算法笔记)

[外链图片转存中…(img-L9JtEoy5-1711133160107)]

(3)面经+真题解析+对应的相关笔记(很全面)

[外链图片转存中…(img-ckVhHA6c-1711133160107)]

(4)视频学习(部分)

ps:当你觉得学不进或者累了的时候,视频是个不错的选择

在这里,最后只一句话:祝大家offer拿到手软!!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 14
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FileUtil是一个基于Qt框架开发的文件操作工具。Qt是一种跨平台的C++应用程序开发框架,可以实现图形用户界面(GUI)以及许多其他功能。 FileUtil提供了许多方便的文件操作功能,可以在不同的操作系统上使用。它可以帮助我们进行文件的创建、复制、移动、重命名、删除以及文件夹的创建和删除等操作。 使用FileUtil,我们可以很方便地在程序中读取和写入文件。我们可以打开一个文件,并通过读取和写入流来读取或写入文件内容FileUtil还提供了一些常用的函数,比如判断文件是否存在、获取文件大小,读取和写入文件时的进度等。 除了文件操作FileUtil还可以进行文件夹的操作。它可以帮助我们创建、删除文件夹,以及遍历文件夹中的文件和子文件夹。 FileUtil还提供了一些其他的功能,比如文件的压缩和解压缩,文件权限的设置等。 作为一个基于Qt的文件操作工具,FileUtil具有跨平台的优势。无论我们是在Windows、Mac还是Linux系统上开发应用程序,都可以使用FileUtil来进行文件操作,而无需关心不同操作系统的差异。 总而言之,FileUtil是一个方便实用的文件操作工具,为我们提供了简单易用的文件操作接口。无论是读取、写入文件,还是进行文件操作FileUtil都能满足我们的需求。使用它,我们可以更方便地对文件进行管理和操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值