• 【跟我学apache-commons】【四】commons-io的使用


    commons-io是一款处理io流的工具,封装了很多处理io流和文件的方法,可以大大简化我们处理io流和操作文件的代码。从common-io的官方使用文档可以看出,它主要分为工具类、尾端类、行迭代器、文件过滤器、文件比较器和扩展流。

    官网地址:http://commons.apache.org/proper/commons-io/

    下载 :http://commons.apache.org/proper/commons-io/download_io.cgi

    一、工具类

    工具类包括FileUtils、IOUtils、FilenameUtils和FileSystemUtils,前三者的方法并没有多大的区别,只是操作的对象不同,故名思议:FileUtils主要操作File类,IOUtils主要操作IO流,FilenameUtils则是操作文件名,FileSystemUtils包含了一些JDK没有提供的用于访问文件系统的实用方法。当前,只有一个用于读取硬盘空余空间的方法可用。实例如下

    FileUtils的使用:

    [java] view plain copy
     
     print?
    1. package com.wj.test;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.util.List;  
    6.   
    7. import org.apache.commons.io.FileUtils;  
    8. import org.junit.After;  
    9. import org.junit.Before;  
    10. import org.junit.Test;  
    11.   
    12. public class FileUtilsTest {  
    13.   
    14.     private String basePath = null;  
    15.   
    16.     @Before  
    17.     public void setUp() {  
    18.         basePath = System.getProperty("user.dir") + "\file\";  
    19.     }  
    20.   
    21.     @After  
    22.     public void tearDown() throws Exception {  
    23.     }  
    24.   
    25.     /** 
    26.      * 拷贝文件 
    27.      * @throws IOException 
    28.      */  
    29.     @Test  
    30.     public void testCopy() throws IOException {  
    31.         File srcFile = new File(basePath + "a.txt");  
    32.         File destFile = new File(basePath + "b.txt");  
    33.         FileUtils.copyFile(srcFile, destFile);  
    34.     }  
    35.       
    36.     /** 
    37.      * 删除文件 
    38.      * @throws IOException 
    39.      */  
    40.     @Test  
    41.     public void testDelete() throws IOException{  
    42.         File delFile = new File(basePath + "b.txt");  
    43.         FileUtils.forceDelete(delFile);  
    44.         //FileUtils.forceMkdir(delFile);  
    45.     }  
    46.       
    47.     /** 
    48.      * 比较文件内容 
    49.      * @throws IOException 
    50.      */  
    51.     @Test  
    52.     public void testCompareFile() throws IOException{  
    53.         File srcFile = new File(basePath + "a.txt");  
    54.         File destFile = new File(basePath + "b.txt");  
    55.         boolean result = FileUtils.contentEquals(srcFile, destFile);  
    56.         System.out.println(result);  
    57.     }  
    58.       
    59.     /** 
    60.      * 移动文件 
    61.      * @throws IOException 
    62.      */  
    63.     @Test  
    64.     public void testMoveFile() throws IOException{  
    65.         File srcFile = new File(basePath + "b.txt");  
    66.         File destDir = new File(basePath + "move");  
    67.         FileUtils.moveToDirectory(srcFile, destDir, true);  
    68.     }  
    69.       
    70.     /** 
    71.      * 读取文件内容 
    72.      * @throws IOException 
    73.      */  
    74.     @Test   
    75.     public void testRead() throws IOException{  
    76.         File srcFile = new File(basePath + "a.txt");  
    77.         String content = FileUtils.readFileToString(srcFile);  
    78.         List<String> contents = FileUtils.readLines(srcFile);  
    79.         System.out.println(content);  
    80.         System.out.println("******************");  
    81.         for (String string : contents) {  
    82.             System.out.println(string);  
    83.         }  
    84.     }  
    85.       
    86.     /** 
    87.      * 写入文件内容 
    88.      * @throws IOException 
    89.      */  
    90.     @Test  
    91.     public void testWrite() throws IOException{  
    92.         File srcFile = new File(basePath + "a.txt");  
    93.         FileUtils.writeStringToFile(srcFile, " yes文件", true);  
    94.     }  
    95.       
    96. }  

    FileSystemUtils的使用:

    [java] view plain copy
     
     print?
    1. package com.wj.test;  
    2.   
    3. import java.io.IOException;  
    4.   
    5. import org.apache.commons.io.FileSystemUtils;  
    6. import org.junit.After;  
    7. import org.junit.Before;  
    8. import org.junit.Test;  
    9.   
    10. public class FileSystemUtilsTest {  
    11.     @Before  
    12.     public void setUp() throws Exception {  
    13.     }  
    14.   
    15.     @After  
    16.     public void tearDown() throws Exception {  
    17.     }  
    18.   
    19.     /** 
    20.      * 获取磁盘空余空间 
    21.      * @throws IOException 
    22.      */  
    23.     @SuppressWarnings("deprecation")  
    24.     @Test  
    25.     public void testFreeSpace() throws IOException {  
    26.         // 以字节为单位  
    27.         System.out.println(FileSystemUtils.freeSpace("c:\") / 1024 / 1024 / 1024);  
    28.         System.out.println(FileSystemUtils.freeSpace("d:\") / 1024 / 1024 / 1024);  
    29.         // 以k为单位  
    30.         System.out.println(FileSystemUtils.freeSpaceKb("e:\") / 1024 / 1024);  
    31.         System.out.println(FileSystemUtils.freeSpaceKb("f:\") / 1024 / 1024);  
    32.           
    33.     }  
    34.   
    35. }  

    二、尾端类

    不同的计算机体系结构使用不同约定的字节排序。在所谓的“低位优先”体系结构中(如Intel),低位字节处于内存中最低位置,而其后的字节,则处于更高的位置。在“高位优先”的体系结构中(如Motorola),这种情况恰恰相反。

    这个类库上有两个相关类:

    EndianUtils包含用于交换java原对象和流之间的字节序列。

    SwappedDataInputStream类是DataInput接口的一个实例。使用它,可以读取非本地的字节序列。

    三、行迭代器

    org.apache.commons.io.LineIterator类提供了一个灵活的方式与基于行的文件交互。可以直接创建一个实例,或者使用FileUtils或IOUtils的工厂方法来创建,实例如下:

    [java] view plain copy
     
     print?
    1. package com.wj.test;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5.   
    6. import org.apache.commons.io.FileUtils;  
    7. import org.apache.commons.io.LineIterator;  
    8. import org.junit.After;  
    9. import org.junit.Before;  
    10. import org.junit.Test;  
    11.   
    12. public class LineIteratorTest {  
    13.       
    14.     private String basePath = null;  
    15.   
    16.     @Before  
    17.     public void setUp() throws Exception {  
    18.         basePath = System.getProperty("user.dir") + "\file\";  
    19.     }  
    20.   
    21.     @After  
    22.     public void tearDown() throws Exception {  
    23.     }  
    24.       
    25.     /** 
    26.      * 测试行迭代器 
    27.      * @throws IOException 
    28.      */  
    29.     @Test  
    30.     public void testIterator() throws IOException{  
    31.         File file = new File(basePath + "a.txt");  
    32.         LineIterator li = FileUtils.lineIterator(file);  
    33.         while(li.hasNext()){  
    34.             System.out.println(li.nextLine());  
    35.         }  
    36.         LineIterator.closeQuietly(li);  
    37.     }  
    38.   
    39. }  



    四、文件过滤器

    org.apache.commons.io.filefilter包定义了一个合并了java.io.FileFilter以及java.io.FilenameFilter的接口(IOFileFilter)。除此之外,这个包还提供了一系列直接可用的IOFileFilter的实现类,可以通过他们合并其它的文件过滤器。比如,这些文件过滤器可以在列出文件时使用或者在使用文件对话框时使用。实例如下:

    [java] view plain copy
     
     print?
    1. package com.wj.test;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5.   
    6. import org.apache.commons.io.filefilter.EmptyFileFilter;  
    7. import org.apache.commons.io.filefilter.SuffixFileFilter;  
    8. import org.junit.After;  
    9. import org.junit.Before;  
    10. import org.junit.Test;  
    11.   
    12. public class FileFilterTest {  
    13.       
    14.     private String basePath = null;  
    15.   
    16.     @Before  
    17.     public void setUp() throws Exception {  
    18.         basePath = System.getProperty("user.dir") + "\file\";  
    19.     }  
    20.   
    21.     @After  
    22.     public void tearDown() throws Exception {  
    23.     }  
    24.       
    25.     /** 
    26.      * 空内容文件过滤器 
    27.      * @throws IOException 
    28.      */  
    29.     @Test  
    30.     public void testEmptyFileFilter() throws IOException{  
    31.         File dir = new File(basePath);  
    32.         String[] files = dir.list(EmptyFileFilter.NOT_EMPTY);  
    33.         for (String file : files) {  
    34.             System.out.println(file);  
    35.         }  
    36.     }  
    37.       
    38.     /** 
    39.      * 文件名称后缀过滤器 
    40.      * @throws IOException 
    41.      */  
    42.     @Test  
    43.     public void testSuffixFileFilter() throws IOException{  
    44.         File dir = new File(basePath);  
    45.         String[] files = dir.list(new SuffixFileFilter("a.txt"));  
    46.         for (String file : files) {  
    47.             System.out.println(file);  
    48.         }  
    49.     }  
    50.   
    51. }  

    五、文件比较器

    org.apache.commons.io.comparator包为java.io.File提供了一些java.util.Comparator接口的实现。例如,可以使用这些比较器对文件集合或数组进行排序。实例如下:

    [java] view plain copy
     
     print?
    1. package com.wj.test;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5.   
    6. import org.apache.commons.io.comparator.CompositeFileComparator;  
    7. import org.apache.commons.io.comparator.DirectoryFileComparator;  
    8. import org.apache.commons.io.comparator.NameFileComparator;  
    9. import org.apache.commons.io.comparator.PathFileComparator;  
    10. import org.junit.After;  
    11. import org.junit.Before;  
    12. import org.junit.Test;  
    13.   
    14. public class ComparatorTest {  
    15.   
    16.     private String basePath = null;  
    17.   
    18.     @Before  
    19.     public void setUp() throws Exception {  
    20.         basePath = System.getProperty("user.dir") + "\file\";  
    21.     }  
    22.   
    23.     @After  
    24.     public void tearDown() throws Exception {  
    25.     }  
    26.   
    27.     /** 
    28.      * 文件名称比较器 
    29.      * @throws IOException 
    30.      */  
    31.     @Test  
    32.     public void testNameFileComparator() throws IOException {  
    33.         File f1 = new File(basePath + "a.txt");  
    34.         File f2 = new File(basePath + "c.txt");  
    35.         int result = NameFileComparator.NAME_COMPARATOR.compare(f1, f2);  
    36.         System.out.println(result);  
    37.     }  
    38.   
    39.     /** 
    40.      * 文件路径比较器 
    41.      * @throws IOException 
    42.      */  
    43.     @Test  
    44.     public void testPathFileComparator() throws IOException {  
    45.         File f1 = new File(basePath + "a.txt");  
    46.         File f2 = new File(basePath + "c.txt");  
    47.         int result = PathFileComparator.PATH_COMPARATOR.compare(f1, f2);  
    48.         System.out.println(result);  
    49.     }  
    50.   
    51.     /** 
    52.      * 组合比较器 
    53.      * @throws IOException 
    54.      */  
    55.     @SuppressWarnings("unchecked")  
    56.     @Test  
    57.     public void testCompositeFileComparator() throws IOException {  
    58.         File dir = new File(basePath);  
    59.         File [] files = dir.listFiles();  
    60.         for (File file : files) {  
    61.             System.out.println(file.getName());  
    62.         }  
    63.         CompositeFileComparator cfc = new CompositeFileComparator(  
    64.                 DirectoryFileComparator.DIRECTORY_COMPARATOR,  
    65.                 NameFileComparator.NAME_COMPARATOR);  
    66.         cfc.sort(files);  
    67.         System.out.println("*****after sort*****");  
    68.         for (File file : files) {  
    69.             System.out.println(file.getName());  
    70.         }  
    71.     }  
    72. }  

    六、扩展流

    org.apache.commons.io.input和org.apache.commons.io.output包中包含的针对数据流的各种各样的的实现。包括:

      • 空输出流-默默吸收发送给它的所有数据
      • T型输出流-全用两个输出流替换一个进行发送
      • 字节数组输出流-这是一个更快版本的JDK类
      • 计数流-计算通过的字节数
      • 代理流-使用正确的方法委拖
      • 可锁写入-使用上锁文件提供同步写入
      • 等等
  • 相关阅读:
    flush table with read lock的轻量级解决方案
    Linux进程关系(转载)
    常用的linux系统监控命令
    MySQL 5.7 InnoDB缓冲池NUMA功能支持——但是别高兴的太早
    网站收集ing....
    【转】程序设计语言中的 一等公民,二等公民,三等公民
    sbt修改为阿里云镜像
    java 中的原始类型与原始封装类型
    【转】MySql中row_number()、rank()、dense_rank() 的区别
    WPS_word使用
  • 原文地址:https://www.cnblogs.com/Jeson2016/p/6871644.html
Copyright © 2020-2023  润新知