commonsIO工具类的使用

1、下载jar文件:commonsIO工具包

2、导入到工程中去

操作

文件和文件夹的大小:FileUtils.sizeOf()

  1. public class io_commonsIO_test {
  2. public static void main(String[] args) {
  3. //文件大小
  4. long len= FileUtils.sizeOf(new File("E:/idea/io_commonsIO_study/io_commonsIO_study.iml"));
  5. System.out.println(len);
  6. //文件夹大小
  7. len=FileUtils.sizeOfDirectory(new File("E:/idea/io_commonsIO_study"));
  8. System.out.println(len);
  9. }
  10. }

遍历子孙级文件

  1. public class io_commonsIO_test02 {
  2. public static void main(String[] args) {
  3. //过滤空文件,null为第一层目录
  4. Collection<File> files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"), EmptyFileFilter.NOT_EMPTY,null);
  5. for (File fs:files
  6. ) {
  7. System.out.println(fs.getAbsolutePath());
  8. }
  9. System.out.println("--------------------------");
  10. //过滤空文件,递归目录
  11. files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"), EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);
  12. for (File fs:files
  13. ) {
  14. System.out.println(fs.getAbsolutePath());
  15. }
  16. System.out.println("--------------------------");
  17. //递归列出后缀为.java的文件
  18. files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"),
  19. new SuffixFileFilter("java"), DirectoryFileFilter.INSTANCE);
  20. for (File fs:files
  21. ) {
  22. System.out.println(fs.getAbsolutePath());
  23. }
  24. System.out.println("--------------------------");
  25. //递归列出后缀为.java和.class的文件和空文件
  26. files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"),
  27. FileFilterUtils.or(new SuffixFileFilter("java"),new SuffixFileFilter("class"),EmptyFileFilter.EMPTY),DirectoryFileFilter.INSTANCE);
  28. for (File fs:files
  29. ) {
  30. System.out.println(fs.getAbsolutePath());
  31. }
  32. System.out.println("--------------------------");
  33. }
  34. }

读取文件

  1. public class io_commonsIO_test03 {
  2. public static void main(String[] args) throws IOException {
  3. //读取文件
  4. String msg =FileUtils.readFileToString(new File("empty.txt"),"utf-8");
  5. System.out.println(msg);
  6. System.out.println("----------------");
  7. //读取文件存入字节数组
  8. byte[]datas=FileUtils.readFileToByteArray(new File("empty.txt"));
  9. System.out.println(datas.length);
  10. System.out.println("------------------");
  11. //逐行读取
  12. List<String>list=FileUtils.readLines(new File("empty.txt"),"utf-8");
  13. for (String sr:list
  14. ) {
  15. System.out.println(sr);
  16. }
  17. System.out.println("---------------");
  18. }
  19. }

写出内容

  1. public class io_commonsIO_test04 {
  2. public static void main(String[] args) throws IOException {
  3. //写出文件
  4. FileUtils.write(new File("happycode.txt"),"山东扒鸡,买一送一!\r\n","utf-8");
  5. FileUtils.writeStringToFile(new File("happycode.txt"),"山东扒鸡,买一送一!\r\n","utf-8",true);
  6. //写出列表
  7. List<String>datas=new ArrayList<>();
  8. datas.add("扒鸡");
  9. datas.add("山扒");
  10. datas.add("德扒");
  11. FileUtils.writeLines(new File("happycode.txt"),datas,"===",true);
  12. }
  13. }

文件拷贝

  1. public class io_commonsIO_test05 {
  2. public static void main(String[] args) throws IOException {
  3. 复制文件
  4. FileUtils.copyFile(new File("io.png"),new File("io_copy.png"));
  5. 复制文件到目录
  6. FileUtils.copyFileToDirectory(new File("io.png"),new File("lib"));
  7. 复制目录到目录,成为子目录
  8. FileUtils.copyDirectoryToDirectory(new File("lib"),new File("lib2"));
  9. 复制目录
  10. FileUtils.copyDirectory(new File("lib"),new File("lib2"));
  11. 拷贝URl内容
  12. String url="http://down-update.qq.com/dnf/website/wallpaper/201907/201907b.rar";
  13. FileUtils.copyURLToFile(new URL(url),new File("C:/Users/tfp12/Desktop/DNF.rar"));
  14. String baidu=IOUtils.toString(new URL("http://www.baidu.com"),"utf-8");
  15. System.out.println(baidu);
  16. }
  17. }