Hutool工具类FileUtil----文件(夹)创建、删除、添加数据

时间:2025-03-16 07:14:35

1.文件(夹)创建

   //创建文件,多级目录会循环创建出来
   String path = "d:/hutool_test/hutool_test.txt";
   File touch = ("d:/hutool_test/hutool_test.txt");

2.文件(夹)的校验

boolean isFile = (path);
 ("isFile:" + isFile);
 boolean isDirectory = (path);
 ("isDirectory:" + isDirectory);

 //空文件和文件夹都可判断
 boolean empty = (new File("d:/hutool_test"));
 ("empty:" + empty);

 boolean notEmpty = (new File(path));
 ("notEmpty:" + notEmpty);

 boolean dirEmpty = (new File("d:/hutool_test"));
 ("dirEmpty:" + dirEmpty);

执行结果:

isFile:true
isDirectory:false
empty:false
notEmpty:false
dirEmpty:false

3.添加数据

File touch = ("d:/hutool_test/hutool_test.txt");
List<String> listParent = new ArrayList<>();
("id  name  age");
("1  xm  18");
("2  xh  28");
//添加数据
FileUtil.writeUtf8Lines(listParent,touch); 
//追加数据
FileUtil.appendUtf8Lines(listParent,touch);

执行结果:

id  name  age
1   xm  18
2   xh  28
id  name  age
1   xm  18
2   xh  28

4.文件(夹)删除

#传递文件路径,删除文件
#传递文件夹路径,删除文件夹及文件夹下文件
 (new File("d:/hutool_test"));