commos_io.jar包下载地址:http://commons.apache.org/proper/commons-io/download_io.cgi

官方文档地址:http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html


//文件、目录遍历 

1 package commosIO;

 import java.io.File;
import java.util.Collection; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.EmptyFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.SuffixFileFilter; public class CommosIOTest1 {
public static void main(String[] args) {
//文件或目录的大小
long len = FileUtils.sizeOf(new File("src/commosIO/CommosIOTest1.java"));
long len2 = FileUtils.sizeOf(new File("F:/EclipseWorkspace/Java300"));
System.out.println(len);
System.out.println(len2);
System.out.println("-----------------------------"); //列出目录下的文件 具体查看api文档
Collection<File> c = FileUtils.listFiles(new File("F:/EclipseWorkspace/Java300"),
EmptyFileFilter.NOT_EMPTY,DirectoryFileFilter.INSTANCE);
for(File temp:c) {
System.out.println(temp.getAbsolutePath());
} //只列出后缀为java的文件 文件过滤
System.out.println("-----------------------------");
Collection<File> c2 = FileUtils.listFiles(new File("F:/EclipseWorkspace/Java300"),
new SuffixFileFilter(".java"),DirectoryFileFilter.INSTANCE);
for(File temp:c2) {
System.out.println(temp.getAbsolutePath());
} System.out.println("-----------------------------");
Collection<File> c3 = FileUtils.listFiles(new File("F:/EclipseWorkspace/Java300"),
FileFilterUtils.or(new SuffixFileFilter(".java"),new SuffixFileFilter(".txt")),
DirectoryFileFilter.INSTANCE);
for(File temp:c3) {
System.out.println(temp.getAbsolutePath());
}
}
} 输出:

1659
6089568
-----------------------------
F:\EclipseWorkspace\Java300\.classpath
F:\EclipseWorkspace\Java300\.project
F:\EclipseWorkspace\Java300\.settings\org.eclipse.jdt.core.prefs
F:\EclipseWorkspace\Java300\a-coyp.jpg
F:\EclipseWorkspace\Java300\a.jpg
F:\EclipseWorkspace\Java300\b.jpg
F:\EclipseWorkspace\Java300\bin\commosIO\CommosIOTest1.class
F:\EclipseWorkspace\Java300\bin\commosIO\CommosIOTest2.class
F:\EclipseWorkspace\Java300\bin\commosIO\CommosIOTest3.class
F:\EclipseWorkspace\Java300\bin\ioStudy\Coffee.class
F:\EclipseWorkspace\Java300\bin\ioStudy\Copy.class
F:\EclipseWorkspace\Java300\bin\ioStudy\Decorate.class
F:\EclipseWorkspace\Java300\bin\ioStudy\DecorateStudy1.class
F:\EclipseWorkspace\Java300\bin\ioStudy\DirStudy.class
F:\EclipseWorkspace\Java300\bin\ioStudy\Drink.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy1.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy2.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy3.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy4.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy5.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy6.class
F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy7.class
F:\EclipseWorkspace\Java300\bin\ioStudy\Milk.class
F:\EclipseWorkspace\Java300\bin\ioStudy\Sugar.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\ArrayCopy1.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\MyArrayList.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\MyHashMap.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\Node.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\TestBubbleSort.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\TestCollection.class
F:\EclipseWorkspace\Java300\bin\test\stringTest\TestString.class
F:\EclipseWorkspace\Java300\commons-io-2.6-sources.jar
F:\EclipseWorkspace\Java300\commons-io-2.6.jar
F:\EclipseWorkspace\Java300\order.txt
F:\EclipseWorkspace\Java300\output.txt
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest1.java
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest2.java
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest3.java
F:\EclipseWorkspace\Java300\src\ioStudy\Copy.java
F:\EclipseWorkspace\Java300\src\ioStudy\DecorateStudy1.java
F:\EclipseWorkspace\Java300\src\ioStudy\DirStudy.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy1.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy2.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy3.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy4.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy5.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy6.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy7.java
F:\EclipseWorkspace\Java300\src\test\stringTest\ArrayCopy1.java
F:\EclipseWorkspace\Java300\src\test\stringTest\MyArrayList.java
F:\EclipseWorkspace\Java300\src\test\stringTest\MyHashMap.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestBubbleSort.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestCollection.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestString.java
F:\EclipseWorkspace\Java300\temp\a.jpg
F:\EclipseWorkspace\Java300\test.txt
F:\EclipseWorkspace\Java300\testcopy.txt
-----------------------------
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest1.java
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest2.java
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest3.java
F:\EclipseWorkspace\Java300\src\ioStudy\Copy.java
F:\EclipseWorkspace\Java300\src\ioStudy\DecorateStudy1.java
F:\EclipseWorkspace\Java300\src\ioStudy\DirStudy.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy1.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy2.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy3.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy4.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy5.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy6.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy7.java
F:\EclipseWorkspace\Java300\src\test\stringTest\ArrayCopy1.java
F:\EclipseWorkspace\Java300\src\test\stringTest\MyArrayList.java
F:\EclipseWorkspace\Java300\src\test\stringTest\MyHashMap.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestBubbleSort.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestCollection.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestString.java
-----------------------------
F:\EclipseWorkspace\Java300\order.txt
F:\EclipseWorkspace\Java300\output.txt
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest1.java
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest2.java
F:\EclipseWorkspace\Java300\src\commosIO\CommosIOTest3.java
F:\EclipseWorkspace\Java300\src\ioStudy\Copy.java
F:\EclipseWorkspace\Java300\src\ioStudy\DecorateStudy1.java
F:\EclipseWorkspace\Java300\src\ioStudy\DirStudy.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy1.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy2.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy3.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy4.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy5.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy6.java
F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy7.java
F:\EclipseWorkspace\Java300\src\test\stringTest\ArrayCopy1.java
F:\EclipseWorkspace\Java300\src\test\stringTest\MyArrayList.java
F:\EclipseWorkspace\Java300\src\test\stringTest\MyHashMap.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestBubbleSort.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestCollection.java
F:\EclipseWorkspace\Java300\src\test\stringTest\TestString.java
F:\EclipseWorkspace\Java300\test.txt
F:\EclipseWorkspace\Java300\testcopy.txt

文件读写

 1 package commosIO;

 import java.io.File;
import java.io.IOException;
import java.util.List; import org.apache.commons.io.FileUtils; public class CommosIOTest2 {
public static void main(String[] args) throws IOException {
//读取文件
String s = FileUtils.readFileToString(new File("output.txt") , "GBK");
System.out.println(s);
System.out.println("-----------------");
byte[] datas = FileUtils.readFileToByteArray(new File("output.txt"));
System.out.println(datas.length);
System.out.println("-----------------");
//逐行读取
List<String> list = FileUtils.readLines(new File("output.txt"),"GBK");
for(String temp:list) {
System.out.println(temp);
}
System.out.println("-----------------");
//写文件 追加
FileUtils.write(new File("output.txt"), "锄禾日当午\r\n","GBK");
FileUtils.write(new File("output.txt"), "汗滴禾下土","GBK",true);
}
} 输出:
锄禾日当午
汗滴禾下土
-----------------
22
-----------------
锄禾日当午
汗滴禾下土
-----------------

文件拷贝

 1 package commosIO;

 import java.io.File;
import java.io.IOException;
import java.net.URL; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; public class CommosIOTest3 {
public static void main(String[] args) throws IOException {
//复制文件
FileUtils.copyFile(new File("a.jpg"), new File("a-coyp.jpg"));
//复制文件到目录
FileUtils.copyFileToDirectory(new File("a.jpg"), new File("temp"));
//复制目录到目录
//复制目录等 查看文档 大同小异 //拷贝url内容
FileUtils.copyURLToFile(new URL("http://www.baidu.com"), new File("order.txt")); String datas = IOUtils.toString(new URL("http://www.baidu.com"),"UTF-8");
System.out.println(datas);
}
}

Commons_IO_FileUtils的使用的更多相关文章

随机推荐

  1. C#中的函数式编程:递归与纯函数(二) 学习ASP.NET Core Razor 编程系列四——Asp.Net Core Razor列表模板页面

    C#中的函数式编程:递归与纯函数(二)   在序言中,我们提到函数式编程的两大特征:无副作用.函数是第一公民.现在,我们先来深入第一个特征:无副作用. 无副作用是通过引用透明(Referential ...

  2. COCOS学习笔记--即时动作ActionInstant

    Cocos引擎中的动作类的关系图例如以下: 能够看出,Action是继承自Ref类的,之前我的博客中也有讲过,Ref类是cocos2dx全部类的基类.动作类(Action)是全部动作的基类.它通过cr ...

  3. SpringMVC上传文件后返回文件服务器地址路径

    先写一个表单: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...

  4. Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                                   D. Painting The Wall ...

  5. go8---函数function

    package main /* 函数function Go 函数 不支持 嵌套.重载和默认参数. 但支持以下特性: 无需声明原型(C语言在使用函数之前需要声明函数的原型).不定长度变参.多返回值.命名 ...

  6. ZOJ3965 Binary Tree Restoring

    ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...

  7. U74201 旅行计划 树上找链长度

    题目背景 珂朵莉放假了,她打算去唐山旅行. 题目描述 我们简单地把唐山的共 nn 个景点看成是一棵树,有 n-1n−1 条边将它们连接起来,每个景点有一个游览指数 v_ivi​.珂朵莉的假期时间不长, ...

  8. bzoj 1705: [Usaco2007 Nov]Telephone Wire 架设电话线【dp】

    i的初始化写成2了于是成功查错2h--怕不是个傻子 设f[i][j]为第i根高为j,转移是 \[ f[i][j]=min(f[i-1][k]+abs(k-j)*c+(j-h[i])^2)(j>= ...

  9. bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】

    最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...

  10. [Swift通天遁地]一、超级工具-(14)使用SweetAlert制作漂亮的自定义Alert窗口

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...