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

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

  • 工具类

  工具类包括FileUtils、IOUtils、FilenameUtils和FileSystemUtils,前三者的方法并没有多大的区别,只是操作的对象不同,故名思议:FileUtils主要操作File类,IOUtils主要操作IO流,FilenameUtils则是操作文件名,

  FileSystemUtils包含了一些JDK没有提供的用于访问文件系统的实用方法。

  FileUtils的使用:

  /**
* 拷贝文件
* @throws IOException
*/
@Test
public void testCopy() throws IOException {
File srcFile = new File(basePath + "a.txt");
File destFile = new File(basePath + "b.txt");
FileUtils.copyFile(srcFile, destFile);
} /**
* 删除文件
* @throws IOException
*/
@Test
public void testDelete() throws IOException{
File delFile = new File(basePath + "b.txt");
FileUtils.forceDelete(delFile);
//FileUtils.forceMkdir(delFile);
} /**
* 比较文件内容
* @throws IOException
*/
@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);
} /**
* 移动文件
* @throws IOException
*/
@Test
public void testMoveFile() throws IOException{
File srcFile = new File(basePath + "b.txt");
File destDir = new File(basePath + "move");
FileUtils.moveToDirectory(srcFile, destDir, true);
} /**
* 读取文件内容
* @throws IOException
*/
@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);
}
} /**
* 写入文件内容
* @throws IOException
*/
@Test
public void testWrite() throws IOException{
File srcFile = new File(basePath + "a.txt");
FileUtils.writeStringToFile(srcFile, "\nyes文件", true);
}
  • 尾端类

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

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

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

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

  • 行迭代器
  /**
* 测试行迭代器
* @throws IOException
*/
@Test
public void testIterator() throws IOException{
File file = new File(basePath + "a.txt");
LineIterator li = FileUtils.lineIterator(file);
while(li.hasNext()){
System.out.println(li.nextLine());
}
LineIterator.closeQuietly(li);
}
  • 文件过滤器
  /**
* 空内容文件过滤器
* @throws IOException
*/
@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);
}
} /**
* 文件名称后缀过滤器
* @throws IOException
*/
@Test
public void testSuffixFileFilter() throws IOException{
File dir = new File(basePath);
String[] files = dir.list(new SuffixFileFilter("a.txt"));
for (String file : files) {
System.out.println(file);
}
}
  • 文件比较器
  /**
* 文件名称比较器
* @throws IOException
*/
@Test
public void testNameFileComparator() throws IOException {
File f1 = new File(basePath + "a.txt");
File f2 = new File(basePath + "c.txt");
int result = NameFileComparator.NAME_COMPARATOR.compare(f1, f2);
System.out.println(result);
} /**
* 文件路径比较器
* @throws IOException
*/
@Test
public void testPathFileComparator() throws IOException {
File f1 = new File(basePath + "a.txt");
File f2 = new File(basePath + "c.txt");
int result = PathFileComparator.PATH_COMPARATOR.compare(f1, f2);
System.out.println(result);
} /**
* 组合比较器
* @throws IOException
*/
@SuppressWarnings("unchecked")
@Test
public void testCompositeFileComparator() throws IOException {
File dir = new File(basePath);
File [] files = dir.listFiles();
for (File file : files) {
System.out.println(file.getName());
}
CompositeFileComparator cfc = new CompositeFileComparator(DirectoryFileComparator.DIRECTORY_COMPARATOR,NameFileComparator.NAME_COMPARATOR);
cfc.sort(files);
System.out.println("*****after sort*****");
for (File file : files) {
System.out.println(file.getName());
}
}
  • 扩展流

空输出流-默默吸收发送给它的所有数据
T型输出流-全用两个输出流替换一个进行发送
字节数组输出流-这是一个更快版本的JDK类
计数流-计算通过的字节数
代理流-使用正确的方法委拖
可锁写入-使用上锁文件提供同步写入
等等

关于Apache Commons-IO的使用的更多相关文章

  1. apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)

    转换InputStream到String, 比如 //引入apache的io包 import org.apache.commons.io.IOUtils; ... ...String str = IO ...

  2. Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils

    1.错误叙述性说明 警告: Could not create JarEntryRevision for [jar:file:/D:/MyEclipse/apache-tomcat-7.0.53/web ...

  3. apache commons io包基本功能

    1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...

  4. java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream(转)

    java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream 使用Tomcat的Manag ...

  5. IO与文件读写---使用Apache commons IO包提高读写效率

    觉得很不错,就转载了, 作者: Paul Lin 首先贴一段Apache commons IO官网上的介绍,来对这个著名的开源包有一个基本的了解:Commons IO is a library of ...

  6. Apache Commons IO入门教程(转)

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  7. [转]Apache Commons IO入门教程

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  8. java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream

    java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream at org.apach ...

  9. Apache Commons IO之FileUtils的常用方法

    Apache Commons IO 在学习io流的时候研究(翻译)了一下这个,只有FileUtils的某些方法,并不全面,还请谅解 org.apache.commons.io 这个包下定义了基于 st ...

  10. apache.commons.io.FileUtils的常用操作

    至于相关jar包可以到官网获取 http://commons.apache.org/downloads/index.html package com.wz.apache.fileUtils; impo ...

随机推荐

  1. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

  2. pycharm 的 使用 设置智能目录 Pycharm 断点跳转及 Step Over/Step Into/Step Out 等的区别

    pycharm  右键点击文件夹  有个mark directiory as  根据需要给目录进行设置 Pycharm调试程序时,有时需要直接从第一个断点跳转至第二个断点,如果还是用单步调试的话就非常 ...

  3. zabbix监控一个机器上的多个java进程的jvm

    一.监控安装部署 1.1 JVM端口配置 (/bqhexin/tomcat/bin/catalina.sh)在安装的tomcat路径,找到catalina.sh文件. vim编辑并添加: catali ...

  4. python3.4+Django+pymysql

    pip install Pymysql 修改app里面的__init__.py import pymysqlpymysql.install_as_MySQLdb()

  5. python + selenium +win32gui + winspy 实现图片上传

    过程:模拟点击上传按钮,打开Windows对话框,编辑栏输入文件路径(或网址)点击确定.网上随便找了一个进行测试. 点击后出现Windows上传对话框 用 winspy 来检测窗口的句柄 python ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM - 改变 HTML

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 调度算法FCFS、SJF和优先权调度的介绍和例题

    调度算法 一.先来先服务FCFS (First Come First Serve) 1.思想: 选择最先进入后备/就绪队列的作业/进程,入主存/分配CPU 2.优缺点 优点:对所有作业/进程公平,算法 ...

  8. PCB上LED指示灯电流、电压总结

    一般指示灯正常发光的电流在10~20mA,低电流LED灯的工作电流在2mA一下,亮度和普通的一样. 压降                    电流 红色         1.82~1.88V     ...

  9. Hadoop的FlieSystem类的使用

    1.使用FileSystem类需要导入jar包 解压hadoop-2.7.7.tar.gz 复制如下三个jar包和lib下所有jar包到项目文件下的lib文件 2.查看文件信息 @Test publi ...

  10. tomcat运行方式详解

    tomcat的运行模式有3种 一.bio(blocking I/O) 即阻塞式I/O操作,表示Tomcat使用的是传统的Java I/O操作(即java.io包及其子包).是基于JAVA的HTTP/1 ...