Java之Apache Commons-IO使用精讲
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使用精讲的更多相关文章
- 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 ...
- java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream(转)
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream 使用Tomcat的Manag ...
- java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream at org.apach ...
- 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. ...
- 报错:maven:java.lang.NoClassDefFoundError: org/apache/commons/io/Charsets
运行报错: maven:java.lang.NoClassDefFoundError: org/apache/commons/io/Charsets 找不到Charsets这个类 上网查了以后,是因为 ...
- java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream异常解决方法
使用Tomcat部署Servlet程序时,单步调试跟踪到: List<FileItem> itemList = sfu.parseRequest(request); 总是会报错:Java. ...
- Java (四)APACHE Commons IO 复制文件
上一篇:Java (三)APACHE Commons IO 常规操作 例1:复制文件 1 import java.io.File; 2 import java.io.IOException; 3 4 ...
- Java (三)APACHE Commons IO 常规操作
上一篇:Java (二)基于Eclipse配置Commons IO的环境 例1:查看文件.文件夹的长度(大小). 1 import java.io.File; 2 3 import org.apach ...
- Java 利用Apache Commons Net 实现 FTP文件上传下载
package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- apache commons io包基本功能
1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...
随机推荐
- PYTHON找色不变移动
import cv2 import aircv as ac import numpy as np def wmhd(sjh): bzz0=0 bzz1=0 bzz2=0 xxa=0 yya=0 xxb ...
- SpringBoot默认首页跳转设置
大家在使用SpringBoot时候会遇到将系统接口入门设置为"/",那么这个就是我们常见的默认首页跳转的设置.解决的方式有两种 第一种方式:controller里添加一个" ...
- Vue+axios的四种异步请求,参数的携带以及接收
Vue中axios发送GET, POST, DELETE, PUT四种异步请求,参数携带和接收问题 web.xml配置如下 1.GET请求 发送GET请求: <!--params是关键字,说明所 ...
- linux U盘安装系统工具usb-creator-gtk
linux平台下U盘安装linux系统的工具.它是ubuntu自带的工具,将建时将覆盖U盘中的所有内容.
- 微信小程序云开发-云存储的应用-识别行驶证
一.准备工作 1.创建云函数identify 2.云函数identify中index.js代码 1 // 云函数入口文件 2 const cloud = require('wx-server-sdk' ...
- Beego和Vue的前后端分离跨域问题处理
VUE封装的请求头(注意请求头,跨域要用到) 路径 utils/mereq.js import request from '@/utils/request' import qs from 'qs' e ...
- Win10强制程序高DPI缩放设置
起因 工作原因,需要在win10上安装数个古老vc版本(vc6,vc2008,vc2010),但是显示器是2K的,DPI缩放有问题 尝试 VC6比较好解决:右键,属性,兼容性,更改高DPI设置,勾选替 ...
- 手把手教你玩转HarmonyOS版地图应用开发
一.导读 7月31日,华为HarmonyOS开发者日将在杭州举行.为了方便更多开发者,高德开放平台地图SDK已在业内率先实现鸿蒙化迁移和重构,全面适配HarmonyOS并面向开发者免费发布.开发者可 ...
- frameset框架在.net网站中的小实现。
一般我们生成网页,为减少代码的开发量,通常将不变的网页部分进行重用.通用为三种方法: 1.frameset框架 2.用户自定义控件 3.母版页(消耗资源大,不追叙) 通常1,2两种方法常用. 1.fr ...
- .NET同步原语Barrier简介
Barrier(屏障)是一种自定义的同步原语(synchronization primitive),它解决了多个线程(参与者)在多个阶段之间的并发和协调问题. 1)多个参与者执行相同的几个阶段的操作 ...