其他分享
首页 > 其他分享> > CommonIO组件的简单应用

CommonIO组件的简单应用

作者:互联网

  1 import java.io.File;
  2 import java.io.IOException;
  3 import java.net.URL;
  4 import java.util.ArrayList;
  5 import java.util.Collection;
  6 import java.util.List;
  7 
  8 import org.apache.commons.io.FileUtils;
  9 import org.apache.commons.io.IOUtils;
 10 import org.apache.commons.io.LineIterator;
 11 import org.apache.commons.io.filefilter.DirectoryFileFilter;
 12 import org.apache.commons.io.filefilter.EmptyFileFilter;
 13 import org.apache.commons.io.filefilter.FileFilterUtils;
 14 import org.apache.commons.io.filefilter.SuffixFileFilter;
 15 
 16 public class CommonDemo {
 17     public static void main(String[] args) throws IOException {
 18         /*
 19          * 文件和文件夹的大小
 20          */
 21         long len=FileUtils.sizeOf(new File("src\\com\\hh\\CommonDemo.java"));
 22         System.out.println(len);
 23         len=FileUtils.sizeOf(new File("F:\\HolidayJavaStudy\\myDemo2"));
 24         System.out.println(len);
 25         /*
 26          *按要求显示文件
 27          */
 28         Collection<File> files=FileUtils.listFiles(new File("F:\\HolidayJavaStudy\\myDemo2"), EmptyFileFilter.NOT_EMPTY, null);
 29         for (File file : files) {
 30             System.out.println(file.getAbsolutePath());
 31         }
 32         System.out.println("------------------------------");
 33         files=FileUtils.listFiles(new File("F:\\HolidayJavaStudy\\myDemo2"), EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);
 34         for (File file : files) {
 35             System.out.println(file.getAbsolutePath());
 36         }
 37         System.out.println("------------------------------");
 38         files=FileUtils.listFiles(new File("F:\\HolidayJavaStudy\\myDemo2"), 
 39                 FileFilterUtils.or(new SuffixFileFilter("java"),new SuffixFileFilter("class")), DirectoryFileFilter.INSTANCE);
 40         for (File file : files) {
 41             System.out.println(file.getAbsolutePath());
 42         }
 43         System.out.println("------------------------------");
 44         files=FileUtils.listFiles(new File("F:\\HolidayJavaStudy\\myDemo2"), 
 45                 FileFilterUtils.and(new SuffixFileFilter("java"),EmptyFileFilter.NOT_EMPTY), DirectoryFileFilter.INSTANCE);
 46         for (File file : files) {
 47             System.out.println(file.getAbsolutePath());
 48         }
 49         /*
 50          * 读取文件
 51          
 52          */
 53         String msg=FileUtils.readFileToString(new File("emp.txt"), "GBK");
 54         System.out.println(msg);
 55         byte[] bys=FileUtils.readFileToByteArray(new File("emp.txt"));
 56         System.out.println(bys.length);
 57         //逐行读取
 58         List<String> list=FileUtils.readLines(new File("emp.txt"),"GBK");
 59         for (String string : list) {
 60             System.out.println(string);
 61         }
 62         //使用迭代器
 63         LineIterator it=FileUtils.lineIterator(new File("emp.txt"),"GBK");
 64         while(it.hasNext()){
 65             System.out.println(it.nextLine());
 66         }
 67         
 68         /*
 69          * 写文件
 70          */
 71         FileUtils.write(new File("utf-8.txt"),"我尼玛", "UTF-8", true);
 72         FileUtils.writeStringToFile(new File("utf-8.txt"),"我尼玛\r\n", "UTF-8", true);
 73         FileUtils.writeByteArrayToFile(new File("utf-8.txt"), "q我尼玛\r\n".getBytes("UTF-8"), true);
 74         //写出列表
 75         List<String> strs=new ArrayList<String>();
 76         strs.add("你好");
 77         strs.add("你妈好");
 78         strs.add("你八号");
 79         FileUtils.writeLines(new File("utf-8.txt"),"UTF-8", strs, "--", true);
 80         
 81         /*
 82          * 拷贝
 83          */
 84         //复制文件
 85         //FileUtils.copyFile(new File("xijinping.jpg"), new File("file/xijinping.jpg"));
 86         //复制文件到目录(将文件复制到这个目录下)
 87         //FileUtils.copyFileToDirectory(new File("xijinping.jpg"), new File("file"));
 88         //复制目录到目录(将源目录复制到目的目录,作为目的目录的子目录)
 89         //FileUtils.copyDirectoryToDirectory(new File("file"), new File("file2"));
 90         //复制目录(将源目录中的文件复制到目的目录)
 91         //FileUtils.copyDirectory(new File("file"), new File("file2"));
 92         //拷贝URL内容
 93         /*String url="http://img.netbian.com/file/2020/0215/1ecf16ccc23b09f82f72f34a19da8632.jpg";
 94         FileUtils.copyURLToFile(new URL(url), new File("file2/dali.jpg"));*/
 95         String s1=IOUtils.toString(new URL("http://www.baidu.com"),"UTF-8");
 96         String s2=IOUtils.toString(new URL("http://www.163.com"),"GBK");
 97         System.out.println(s1);
 98         System.out.println(s2);
 99         
100     }
101 
102 }

 

标签:应用,println,System,FileUtils,File,CommonIO,组件,new,out
来源: https://www.cnblogs.com/abtious/p/12331896.html