【跟我学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的使用:
- package com.wj.test;
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import org.apache.commons.io.FileUtils;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class FileUtilsTest {
- private String basePath = null;
- @Before
- public void setUp() {
- basePath = System.getProperty("user.dir") + "\\file\\";
- }
- @After
- public void tearDown() throws Exception {
- }
- /**
- * 拷贝文件
- * @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);
- }
- }
FileSystemUtils的使用:
- package com.wj.test;
- import java.io.IOException;
- import org.apache.commons.io.FileSystemUtils;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class FileSystemUtilsTest {
- @Before
- public void setUp() throws Exception {
- }
- @After
- public void tearDown() throws Exception {
- }
- /**
- * 获取磁盘空余空间
- * @throws IOException
- */
- @SuppressWarnings("deprecation")
- @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);
- }
- }
二、尾端类
不同的计算机体系结构使用不同约定的字节排序。在所谓的“低位优先”体系结构中(如Intel),低位字节处于内存中最低位置,而其后的字节,则处于更高的位置。在“高位优先”的体系结构中(如Motorola),这种情况恰恰相反。
这个类库上有两个相关类:
EndianUtils包含用于交换java原对象和流之间的字节序列。
SwappedDataInputStream类是DataInput接口的一个实例。使用它,可以读取非本地的字节序列。
三、行迭代器
org.apache.commons.io.LineIterator类提供了一个灵活的方式与基于行的文件交互。可以直接创建一个实例,或者使用FileUtils或IOUtils的工厂方法来创建,实例如下:
- package com.wj.test;
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.LineIterator;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class LineIteratorTest {
- private String basePath = null;
- @Before
- public void setUp() throws Exception {
- basePath = System.getProperty("user.dir") + "\\file\\";
- }
- @After
- public void tearDown() throws Exception {
- }
- /**
- * 测试行迭代器
- * @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);
- }
- }
四、文件过滤器
org.apache.commons.io.filefilter包定义了一个合并了java.io.FileFilter以及java.io.FilenameFilter的接口(IOFileFilter)。除此之外,这个包还提供了一系列直接可用的IOFileFilter的实现类,可以通过他们合并其它的文件过滤器。比如,这些文件过滤器可以在列出文件时使用或者在使用文件对话框时使用。实例如下:
- package com.wj.test;
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.io.filefilter.EmptyFileFilter;
- import org.apache.commons.io.filefilter.SuffixFileFilter;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class FileFilterTest {
- private String basePath = null;
- @Before
- public void setUp() throws Exception {
- basePath = System.getProperty("user.dir") + "\\file\\";
- }
- @After
- public void tearDown() throws Exception {
- }
- /**
- * 空内容文件过滤器
- * @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);
- }
- }
- }
五、文件比较器
org.apache.commons.io.comparator包为java.io.File提供了一些java.util.Comparator接口的实现。例如,可以使用这些比较器对文件集合或数组进行排序。实例如下:
- package com.wj.test;
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.io.comparator.CompositeFileComparator;
- import org.apache.commons.io.comparator.DirectoryFileComparator;
- import org.apache.commons.io.comparator.NameFileComparator;
- import org.apache.commons.io.comparator.PathFileComparator;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class ComparatorTest {
- private String basePath = null;
- @Before
- public void setUp() throws Exception {
- basePath = System.getProperty("user.dir") + "\\file\\";
- }
- @After
- public void tearDown() throws Exception {
- }
- /**
- * 文件名称比较器
- * @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());
- }
- }
- }
六、扩展流
org.apache.commons.io.input和org.apache.commons.io.output包中包含的针对数据流的各种各样的的实现。包括:
- 空输出流-默默吸收发送给它的所有数据
- T型输出流-全用两个输出流替换一个进行发送
- 字节数组输出流-这是一个更快版本的JDK类
- 计数流-计算通过的字节数
- 代理流-使用正确的方法委拖
- 可锁写入-使用上锁文件提供同步写入
- 等等
【跟我学apache-commons】【四】commons-io的使用的更多相关文章
- HTTP Status 500 - org.apache.jasper.JasperException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException
HTTP Status 500 - org.apache.jasper.JasperException: com.sun.org.apache.xerces.internal.impl.io.Malf ...
- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte ...
- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...
- [老老实实学WCF] 第四篇 初探通信--ChannelFactory
老老实实学WCF 第四篇 初探通信--ChannelFactory 通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了. ...
- 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计
本帖最后由 xinxincaijq 于 2013-1-9 10:27 编辑 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计 转自博客:http:// ...
- 从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)
原文:从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库) 1.安装对应dll Update-Package Xama ...
- 【原创】大叔问题定位分享(16)spark写数据到hive外部表报错ClassCastException: org.apache.hadoop.hive.hbase.HiveHBaseTableOutputFormat cannot be cast to org.apache.hadoop.hive.ql.io.HiveOutputFormat
spark 2.1.1 spark在写数据到hive外部表(底层数据在hbase中)时会报错 Caused by: java.lang.ClassCastException: org.apache.h ...
- (素材源代码)猫猫学IOS(四)UI之半小时搞定Tom猫
下载地址:http://download.csdn.net/detail/u013357243/8514915 以下是执行图片展示 制作思路以及代码解析 猫猫学IOS(四)UI之半小时搞定Tom猫这里 ...
- hive orc压缩数据异常java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.hive.ql.io.orc.OrcSerde$OrcSerdeRow
hive表在创建时候指定存储格式 STORED AS ORC tblproperties ('orc.compress'='SNAPPY'); 当insert数据到表时抛出异常 Caused by: ...
- HDU 6467 简单数学题 【递推公式 && O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others) M ...
随机推荐
- grunt-inline:一个资源内嵌插件
一.插件简介 将引用的外部资源,如js.css.img等,内嵌到引用它们的文件里去. 二.使用场景 在项目中,出于某些原因,有的时候我们需要将一些资源,比如js脚本内嵌到页面中去.比如我们的html页 ...
- 外网主机访问虚拟机下的web服务器(NAT端口转发)-----端口映射
主机:系统win7,ip地址172.18.186.210 虚拟机:VMware Workstation 7,虚拟机下安装了Centos操作系统,ip地址是192.168.202.128,部署了LAMP ...
- 管理idea Open Recent
在微服务开发过程中,随着服务的增加,日常需要在各个服务之间切换,这样idea 的 Open Recent 功能就显得特别有用,但是过多的最近打开记录中包括已经删除的工程或者无用的工程导致影响开发时切换 ...
- 绕过用编码方式阻止XSS攻击的几个例子
阻止攻击的常用方法是:在将HTML返回给Web浏览器之前,对攻击者输入的HTML进行编码.HTML编码使用一些没有特定HTML意义的字符来代替那些标记字符(如尖括号).这些替代字符不会影响文本在web ...
- Nmap命令
命令行:(显示扫描过程 -v ) 扫描单个主机 #nmap www.hostName.com 扫描整个ip段(子网) #nmap 192.168.1.1/24 //表示当前ip下的24位掩码主机都要扫 ...
- 《Linux内核分析》 之 计算机是如何工作的
[李行之原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] <Linux内 ...
- iOS国际化——通过脚本使storyboard翻译自增
一. 针对两种文件的国际化处理 代码中即.m文件的国际化 首先在你需要进行国际化处理的字符串外面加一层NSLocalizedString,注意中文也是可以的哦 textfield.text = [NS ...
- 联想一体机怎么设置u盘启动|联想一体机bios改U盘启动方法(转)
本文转自:http://www.xitongcheng.com/jiaocheng/xtazjc_article_29090.html 所需工具: 1.联想Lenovo品牌一体机 2.启动U盘:大白菜 ...
- Perfmon - Windows 自带系统监测工具(转)
本文转自:http://blog.csdn.net/oscar999/article/details/7918385 一. 简述 可以用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Pe ...
- Linux命令(二十三) 磁盘管理命令(一) df,du,tune2fs
一. 查看磁盘占用空间情况 df df 命令用于查看硬盘空间的使用情况,还可以查看硬盘分区的类型或 inode 节点的使用情况等. df 命令常用参数如下: -a 显示所有文件系统的磁盘使用情况,包括 ...