how2j.cn

下载区
文件名 文件大小
hutool.rar 20k
步骤 1 : 文件IO工具   
步骤 2 : IOUtil   
步骤 3 : FileUtil   
步骤 4 : WatchMonitor   
步骤 5 : ClassPathResource   
步骤 6 : 可运行项目   

Hutool 里的文件IO工具有好几个

IOUtil :操作流的

FileUtil :操作文件的

FileTypeUtil: 被包含在FileUtil里了

FileReader,FileWriter:这两个基本上都在FileUtil里用了

WatchMonitor: 用来监控文件变化, 这个比较有意思,可以看看自己c盘,平时文件都有什么变化。

ClassPathResource 是class 路径下资源的获取
IOUtil 提供的方法很多,站长把常用的列出来一下,其他不怎么常用的,有兴趣的就自己看api吧

把输入流的数据复制到输出流中

long copy(InputStream in, OutputStream out)

读取输入流的内容为字符串

String read(InputStream in, String charsetName)

把数据写出到输出流中

void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents)
是否是windows系统 (通过分隔符进行判断)

boolean isWindows

追加数据

File appendString(String content, File file, String charset)

遍历当前目录及其子目录

List<File> loopFiles(String path)

目录及其子目录所有文件的大小总和

long size(File file)

创建文件,会自动创建父文件夹。 方法名故意用linux下的命令名

File touch(String fullFilePath)

删除文件或者目录(这个很危险,会自动删除当前目录以及子目录,慎用)

boolean del(String fullFileOrDirPath)

同上

static boolean clean(String dirPath)

复制文件或者目录

File copyFile(String src, String dest, StandardCopyOption... options)

判断俩文件内容是否一样

boolean contentEquals(File file1, File file2)

获取后缀名,不带.

String extName(String fileName)

根据文件头部信息获取文件类型

String getType(File file)

读取内容为字符串

readString(String path, String charsetName)

读取内容为集合

List<String> readLines(String path, String charset)

把字符串写入到文件

File writeString(String content, String path, String charset)

把集合写入到文件

File writeLines(Collection<T> list, File file, String charset)

转换文件编码,第一个参数必须和文件本身编码保持一致,否则就会出错。比如一个文件是GBK的,但是在UTF默认编码的环境下看到都是乱码,就可以通过这个转换一下

File convertCharset(File file, Charset srcCharset, Charset destCharset)

指定换行符,有些文件从Linux搞来的,在window下换行会混乱,可以用这个进行转换

convertLineSeparator(File file, Charset charset, LineSeparator lineSeparator)

获取CRC32校验码

long checksumCRC32(File file)

获取Web项目下的web root路径

File getWebRoot()

根据文件后缀名 (不一定是真实格式),获取其mimetype

String getMimeType(String filePath)
这是监控 F:/game 目录下所有文件的增删改。
站长启动了 "They Are Billions", 就观察到了这么一些文件变化,哈哈~
WatchMonitor
package cn.how2j; import java.io.File; import java.nio.file.Path; import java.nio.file.WatchEvent; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.watch.WatchMonitor; import cn.hutool.core.io.watch.Watcher; import cn.hutool.core.lang.Console; public class TestWatchMonitor { public static void main(String[] args) { File file = FileUtil.file("F:/game"); //这里只监听文件或目录的修改事件 WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.EVENTS_ALL); watchMonitor.setWatcher(new Watcher(){ @Override public void onCreate(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); Console.log("创建:{}-> {}", currentPath, obj); } @Override public void onModify(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); Console.log("修改:{}-> {}", currentPath, obj); } @Override public void onDelete(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); Console.log("删除:{}-> {}", currentPath, obj); } @Override public void onOverflow(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); Console.log("Overflow:{}-> {}", currentPath, obj); } }); //设置监听目录的最大深入,目录层级大于制定层级的变更将不被监听,默认只监听当前层级目录 watchMonitor.setMaxDepth(Integer.MAX_VALUE); //启动监听 watchMonitor.start(); } }
步骤 5 :

ClassPathResource

edit
ClassPathResource 是专门用来读取classpath 里的数据的,很方便。
如代码所所示,读取 hutoool jar 包里的:META-INF/MANIFEST.MF 文件并打印出来了。。。

如果不是用ClassPathResource ,要做还略麻烦的呢
ClassPathResource
package cn.how2j.test; import org.junit.Test; import cn.hutool.core.io.resource.ClassPathResource; public class TestFile { @Test public void test1() { ClassPathResource resource = new ClassPathResource("META-INF/MANIFEST.MF"); System.out.println(resource.readUtf8Str()); } }
package cn.how2j.test;

import org.junit.Test;

import cn.hutool.core.io.resource.ClassPathResource;

public class TestFile {

	@Test
	public void test1() {
		ClassPathResource resource = new ClassPathResource("META-INF/MANIFEST.MF");
		System.out.println(resource.readUtf8Str());
	}
}
在右上角有本知识点对应的可运行项目下载 ,实在自己搞不出来,就下载解压出来比较一下。


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 应用-hutool-文件IO工具 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图