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

1.文件(夹)创建

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

2.文件(夹)的校验

boolean isFile = FileUtil.isFile(path);
 System.out.println("isFile:" + isFile);
 boolean isDirectory = FileUtil.isDirectory(path);
 System.out.println("isDirectory:" + isDirectory);

 //空文件和文件夹都可判断
 boolean empty = FileUtil.isEmpty(new File("d:/hutool_test"));
 System.out.println("empty:" + empty);

 boolean notEmpty = FileUtil.isNotEmpty(new File(path));
 System.out.println("notEmpty:" + notEmpty);

 boolean dirEmpty = FileUtil.isDirEmpty(new File("d:/hutool_test"));
 System.out.println("dirEmpty:" + dirEmpty);

执行结果:

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

3.添加数据

File touch = FileUtil.touch("d:/hutool_test/hutool_test.txt");
List<String> listParent = new ArrayList<>();
listParent.add("id  name  age");
listParent.add("1  xm  18");
listParent.add("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.文件(夹)删除

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

你可能感兴趣的:(HuTool,JAVA,java,hutool)