Commons_IO_FileUtils的使用

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; 2 3 import java.io.File; 4 import java.util.Collection; 5 6 import org.apache.commons.io.FileUtils; 7 import org.apache.commons.io.filefilter.DirectoryFileFilter; 8 import org.apache.commons.io.filefilter.EmptyFileFilter; 9 import org.apache.commons.io.filefilter.FileFilterUtils; 10 import org.apache.commons.io.filefilter.SuffixFileFilter; 11 12 public class CommosIOTest1 { 13 public static void main(String[] args) { 14 //文件或目录的大小 15 long len = FileUtils.sizeOf(new File("src/commosIO/CommosIOTest1.java")); 16 long len2 = FileUtils.sizeOf(new File("F:/EclipseWorkspace/Java300")); 17 System.out.println(len); 18 System.out.println(len2); 19 System.out.println("-----------------------------"); 20 21 //列出目录下的文件 具体查看api文档 22 Collection<File> c = FileUtils.listFiles(new File("F:/EclipseWorkspace/Java300"), 23 EmptyFileFilter.NOT_EMPTY,DirectoryFileFilter.INSTANCE); 24 for(File temp:c) { 25 System.out.println(temp.getAbsolutePath()); 26 } 27 28 //只列出后缀为java的文件 文件过滤 29 System.out.println("-----------------------------"); 30 Collection<File> c2 = FileUtils.listFiles(new File("F:/EclipseWorkspace/Java300"), 31 new SuffixFileFilter(".java"),DirectoryFileFilter.INSTANCE); 32 for(File temp:c2) { 33 System.out.println(temp.getAbsolutePath()); 34 } 35 36 System.out.println("-----------------------------"); 37 Collection<File> c3 = FileUtils.listFiles(new File("F:/EclipseWorkspace/Java300"), 38 FileFilterUtils.or(new SuffixFileFilter(".java"),new SuffixFileFilter(".txt")), 39 DirectoryFileFilter.INSTANCE); 40 for(File temp:c3) { 41 System.out.println(temp.getAbsolutePath()); 42 } 43 } 44 }

输出:

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
View Code

 

 


文件读写

1
package commosIO; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.List; 6 7 import org.apache.commons.io.FileUtils; 8 9 public class CommosIOTest2 { 10 public static void main(String[] args) throws IOException { 11 //读取文件 12 String s = FileUtils.readFileToString(new File("output.txt") , "GBK"); 13 System.out.println(s); 14 System.out.println("-----------------"); 15 byte[] datas = FileUtils.readFileToByteArray(new File("output.txt")); 16 System.out.println(datas.length); 17 System.out.println("-----------------"); 18 //逐行读取 19 List<String> list = FileUtils.readLines(new File("output.txt"),"GBK"); 20 for(String temp:list) { 21 System.out.println(temp); 22 } 23 System.out.println("-----------------"); 24 //写文件 追加 25 FileUtils.write(new File("output.txt"), "锄禾日当午\r\n","GBK"); 26 FileUtils.write(new File("output.txt"), "汗滴禾下土","GBK",true); 27 } 28 }

输出:
锄禾日当午
汗滴禾下土
-----------------
22
-----------------
锄禾日当午
汗滴禾下土
-----------------
View Code

 

 


文件拷贝

1
package commosIO; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.net.URL; 6 7 import org.apache.commons.io.FileUtils; 8 import org.apache.commons.io.IOUtils; 9 10 11 12 public class CommosIOTest3 { 13 public static void main(String[] args) throws IOException { 14 //复制文件 15 FileUtils.copyFile(new File("a.jpg"), new File("a-coyp.jpg")); 16 //复制文件到目录 17 FileUtils.copyFileToDirectory(new File("a.jpg"), new File("temp")); 18 //复制目录到目录 19 //复制目录等 查看文档 大同小异 20 21 //拷贝url内容 22 FileUtils.copyURLToFile(new URL("http://www.baidu.com"), new File("order.txt")); 23 24 String datas = IOUtils.toString(new URL("http://www.baidu.com"),"UTF-8"); 25 System.out.println(datas); 26 } 27 }

 

posted @ 2019-06-05 11:49  Practical  阅读(919)  评论(0编辑  收藏  举报