commons-io操作

commons-io是一款初六io流的工具,封装了很多处理io流和文件的方法,可以大大简化我们处理io流和操作文件的代码.

commons-io主要分为,工具类,尾端类,迭代器,文件过滤器等.

1.工具类

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

a.FileUtils

public class FileUtilsTest {
    private String basePath = null;
    
    @Before
    public void init() {
        basePath = "F:\\test\\";
    }
    /**
     * 拷贝文件
     */
    @Test
    public void testCopy() throws IOException {
        File srcFile = new File(basePath + "a.txt");
        File destFile = new File(basePath + "b.txt");
        FileUtils.copyFile(srcFile, destFile);
    }
    
    /**
     * 删除文件(递归删除)
     */
    @Test
    public void testDelete() throws Exception {
        File destFile = new File(basePath + "access");
        FileUtils.forceDelete(destFile);
    }
    
    /**
     * 比较内容
     */
    @Test
    public void testCompareFile() throws IOException {
        File srcFile = new File(basePath + "a.txt");
        File destFile = new File(basePath + "b.txt");
        boolean result = FileUtils.contentEquals(srcFile, destFile);
        System.out.println(result);
    }
    
    /**
     * 读取文件
     */
    @Test
    public void testRead() throws IOException {
        File srcFile = new File(basePath + "a.txt");
        String content = FileUtils.readFileToString(srcFile);
        List<String> contents = FileUtils.readLines(srcFile);
        System.out.println(content);
        System.out.println("*******************************");
        for(String string:contents) {
            System.out.println(string);
        }
    }
    
    /**
     * 写入文件内容(如果文件不存在会自动创建)
     */
    @Test
    public void testWrite() throws Exception {
        File srcFile = new File(basePath + "c.txt");
        FileUtils.writeStringToFile(srcFile, "xxxxxxxxxxxxxx","UTF-8");
        
        
    }
}

其他几个类似

b.FileSystemUtils

public class FileSystemUtilsTest {
    
    private String basePath = null;
    
    @Before
    public void init() {
        basePath = "F:\\test\\";
    }
    /**
     * 获取磁盘剩余空间
     */
    @Test
    public void testFreeSpace() throws IOException {
        // 以字节为单位  
        System.out.println(FileSystemUtils.freeSpace("c:\\") / 1024 / 1024 / 1024);  
        System.out.println(FileSystemUtils.freeSpace("d:\\") / 1024 / 1024 / 1024);  
        // 以k为单位  
        System.out.println(FileSystemUtils.freeSpaceKb("e:\\") / 1024 / 1024);  
        System.out.println(FileSystemUtils.freeSpaceKb("f:\\") / 1024 / 1024);  
    }
}

 

2.尾端类

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

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

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

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

3.迭代器类

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

public class LineIteratorTest {
    
    private String basePath = null;
    @Before
    public void init() {
        basePath = "F:\\test\\";
    }
    @Test
    public void testIterator() throws IOException {
        File file = new File(basePath + "a.txt");
        LineIterator iterator = FileUtils.lineIterator(file);
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

4.文件过滤器

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

public class FileFilterTest {
    private String basePath = null;
    @Before
    public void init() {
        basePath = "F:\\test\\";
    }
    
    /**
     * 空文件过滤
     */
    @Test
    public void testEmptyFileFilter() throws IOException {
        File dir = new File(basePath);
        String[] files = dir.list(EmptyFileFilter.NOT_EMPTY);
        for(String file:files) {
            System.out.println(file);
        }
    }
    /**
     * 文件名称过滤
     */
    @Test
    public void testSuffixFileFilter() throws IOException {
        File dir = new File(basePath);
        String[] files = dir.list(new SuffixFileFilter(".txt"));
        for(String file:files) {
            System.out.println(file);
        }
    }
}

 

posted @ 2018-08-28 15:05  刘丽刚  阅读(1161)  评论(0编辑  收藏  举报