18.Apache IO 包的使用

1.FileUtils

1-1:readFileToString(读取文件的字符串)

import org.apache.commons.io.FileUtils;

import java.io.File;

public class Dome22 {
   public static void main(String[] args)throws Exception {
     String content =   FileUtils.readFileToString(new File("d:/a.txt"),"utf-8");
       System.out.println(content);
  }
}

 

1-2:copyDirectory(拷贝目录内容到另一个目录)

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileFilter;

public class Dome22 {
   public static void main(String[] args) throws Exception {
       FileUtils.copyDirectory(new File("d:/a"), new File("d:/b"), new FileFilter() {
           @Override
           public boolean accept(File pathname) {
               //拷贝文件的过滤条件
               if (pathname.isDirectory() || pathname.getName().endsWith(".html")) {//判断是不是目录 或者 是不是html结尾的文件
                   return true;
              }
               return false;
          }
      });
  }
}

 

2.IOUtils

2-1:toString(将输入流或字节数组中的内容转化为字符串)

import org.apache.commons.io.IOUtils;
import java.io.FileInputStream;

public class Dome22 {
   public static void main(String[] args) throws Exception {
      String content = IOUtils.toString(new FileInputStream("d:/a.txt"),"utf-8");
      System.out.printf(content);
  }

 

 这里只选了一些作为演示
posted @ 2022-04-23 14:08  阳光下的承诺  阅读(130)  评论(0)    收藏  举报