Commons IO是针对开发IO流功能的工具类库。
主要包括六个区域:

工具类——使用静态方法执行共同任务
输入——用于InputStream和Reader实现
输出——用于OutputStream和Writer实现
过滤器——各种文件过滤器实现
比较器——各种文件的java.util.Comparator实现
文件监听器——监听文件系统事件的组件

工具类

IOUtils

该工具类可能是平时使用得最多的工具类了。
IOUtils包含处理读、写和复制的工具方法。方法对InputStream、OutputStream、Reader和Writer起作用。

例如,从一个URL读取字节的任务,并且打印它们:

    public static void main(String[] args) throws Exception {
//从网络上读取一个网页资源
InputStream in = new URL("http://commons.apache.org").openStream();
try {
InputStreamReader inR = new InputStreamReader(in);
BufferedReader buf = new BufferedReader(inR);
String line;
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
} finally {
if (in != null) {
in.close();
}
}
}
//结果:
//控制台打印出了这个网页的所有内容

使用IOUtils:

    public static void main(String[] args) throws Exception {
//从网络上读取一个网页资源
try (InputStream in = new URL("http://commons.apache.org").openStream()) {
System.out.println(IOUtils.toString(in, StandardCharsets.UTF_8));
}
//finally {
// IOUtils.closeQuietly(in);
//}
}

主要方式介绍:

buffer:一句话可以吧inputStream、outputStream、Reader、Witter等包装成带缓冲区的流,提高效率
closeQuietly:可以关闭各种流、socket等任何closeable的实例(不过官方推荐使用try-with-resources来代替)
contentEquals:比较两个InputStream或者两个Reader里面的内容(字节流)是否完全相同

    public static void main(String[] args) throws Exception {
try (InputStream in1 = new URL("http://commons.apache.org").openStream(); InputStream in2 = new URL("http://commons.apache.org").openStream()) {
System.out.println(in1.equals(in2)); //false
System.out.println(IOUtils.contentEquals(in1, in2)); //true
}
}

备注:contentEqualsIgnoreEOL(final Reader input1, final Reader input2) 该方法会忽略ignoring EOL characters

copy:流的互相拷贝。可以将输入流拷到输出流。copy(final InputStream input, final OutputStream output, final int bufferSize),Reader拷贝到Writer等等
copyLarge:当你的流拷贝的是大文件(一般大于2G级别),请使用此方法拷贝
lineIterator:BufferedReader 通常在只有读到空格或者换行符时才会结束读取,攻击者很容易构内存攻击导致系统瘫痪,出于安全考虑这里推荐使用io包的LineIterator,并且其在性能上也优于普通流。

lineIterator(final InputStream input, final Charset encoding)
lineIterator(final InputStream input, final String encoding)
lineIterator(final Reader reader)
    public static void main(String[] args) throws Exception {
try (InputStream in1 = new URL("http://commons.apache.org").openStream()) {
LineIterator lineIterator = IOUtils.lineIterator(in1, StandardCharsets.UTF_8);
while (lineIterator.hasNext()) {
lineIterator.nextLine();
}
lineIterator.close();
}
}
read、readFully:把输入流的东西读取添加到第二个参数中的字节数组里
readLines:不解释
resourceToByteArray、resourceToString:直接传入一个文件的路径,读取进来
toBufferedInputStream:把普通的inputStream转换成带缓冲区的,返回一个新的InputStream
toByteArray:吧输入流转换到字节数组
toCharArray:
toInputStream:吧字符、字符串等等直接读到流里
toString:强大的方法,可以吧各种输出流读成一个串
write、writeChunked、writeLines:把传入的字节数组,写入到输出流里(可指定编码)

各种常用的常量:

public static final char DIR_SEPARATOR_UNIX = '/';
public static final char DIR_SEPARATOR_WINDOWS = '\\';
/**
* The system directory separator character. 系统文件夹的分隔符 通用的
*/
public static final char DIR_SEPARATOR = File.separatorChar;
/**
* The Unix line separator string. 换行符
*/
public static final String LINE_SEPARATOR_UNIX = "\n";
//winows换行符
public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
/**
* The system line separator string. 通用的换行符
*/
public static final String LINE_SEPARATOR;

FilenameUtils

FilenameUtils类包含工具方法不需要使用File对象就可以操作文件名。该类致力于屏蔽Unix和Windows之间的不同,避免这些环境之间的转换(例如,从开发到生产)。 开发在windows、生产在Linux

一般使用较少,这里不做过多介绍.

FileSystemUtils:2.6版本已经废弃。推荐使用JDK自己的FileStore代替

Java之Apache Commons-IO使用精讲的更多相关文章

  1. 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 ...

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

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

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

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

  4. Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils

    关键词:IntelliJ IDEA.Tomcat.commons-io-2.5.jar.java.lang.ClassNotFoundException: org.apache.commons.io. ...

  5. 报错:maven:java.lang.NoClassDefFoundError: org/apache/commons/io/Charsets

    运行报错: maven:java.lang.NoClassDefFoundError: org/apache/commons/io/Charsets 找不到Charsets这个类 上网查了以后,是因为 ...

  6. java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream异常解决方法

    使用Tomcat部署Servlet程序时,单步调试跟踪到: List<FileItem> itemList = sfu.parseRequest(request); 总是会报错:Java. ...

  7. Java (四)APACHE Commons IO 复制文件

    上一篇:Java (三)APACHE Commons IO 常规操作 例1:复制文件 1 import java.io.File; 2 import java.io.IOException; 3 4 ...

  8. Java (三)APACHE Commons IO 常规操作

    上一篇:Java (二)基于Eclipse配置Commons IO的环境 例1:查看文件.文件夹的长度(大小). 1 import java.io.File; 2 3 import org.apach ...

  9. Java 利用Apache Commons Net 实现 FTP文件上传下载

    package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  10. apache commons io包基本功能

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

随机推荐

  1. python all any函数(相反)

    ''' all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False. 元素除了是 0.空.FALSE 外都算 TRUE. 语 ...

  2. 做词云时报错cannot import name ‘WordCloud‘ from partially initialized module ‘wordcloud‘的解决办法

    问题: 在做词云时,运行时出现该问题,wordcloud安装成功,但运行出错,错误提示是:cannot import name 'WordCloud' from partially initializ ...

  3. 三、从GitHub浏览Prism示例代码的方式入门WPF下的Prism之Mvvm的08-12示例

    这一篇是学习了前2篇RegionManager关联视图,和通过不同的方式加载Module示例之后的开始进入MVVM了. 从第08示例开始,进入了MVVM部分. 从08示例开始学习Prism下的MVVM ...

  4. Unittest方法 -- 测试断言

    """断言详解"""from unittest_1.it import *def add(a,b): return a - bclass B ...

  5. C++:第一个c++程序

    // C++ 环境搭建: https://www.bilibili.com/video/BV1nt4y1r7Ez?t=535 // 学习资料:https://www.runoob.com/cplusp ...

  6. ZYNQ Linux 移植:包含petalinux移植和手动移植debian9

    参考: https://electronut.in/workflow-for-using-linux-on-xilinx-zynq/ https://blog.csdn.net/m0_37545528 ...

  7. Laravel 6 – 搭建管理后台的用户认证“脚手架”工具

    1. 下载Laravel/ui 命令: composer require laravel/ui "^1.0" -dev 注意laravel framework 6只支持版本1的la ...

  8. Linux bash命令行常用快捷键(Xshell和secure CRT以及gnome-terminal)

    常用的命令行击键操作 ctrl + insert   xshell中复制,可以设置选中内容自动复制ctrl shift + c crt中复制shift + insert xshell中粘贴ctrl s ...

  9. python读取csv,Excel,Txt,Yaml 文件

    1.数据 1.Csv login.csv文件: byhy,88888888 ReadCsv.py文件 import csv #导入csv包 class ReadCsv(): def csv(self) ...

  10. jvm源码解读--01 jvm加载java/lang/object过程

    现在做一下记录,这个看了两天,看的过程发现了很多c++的高级特性,没接触过,还得慢慢撸,禁止很慢 那么现在开始 吧 先打两个断点 java.c:351 JavaMain(void * _args) { ...