• 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:EclipseWorkspaceJava300.classpath
    F:EclipseWorkspaceJava300.project
    F:EclipseWorkspaceJava300.settingsorg.eclipse.jdt.core.prefs
    F:EclipseWorkspaceJava300a-coyp.jpg
    F:EclipseWorkspaceJava300a.jpg
    F:EclipseWorkspaceJava300.jpg
    F:EclipseWorkspaceJava300incommosIOCommosIOTest1.class
    F:EclipseWorkspaceJava300incommosIOCommosIOTest2.class
    F:EclipseWorkspaceJava300incommosIOCommosIOTest3.class
    F:EclipseWorkspaceJava300inioStudyCoffee.class
    F:EclipseWorkspaceJava300inioStudyCopy.class
    F:EclipseWorkspaceJava300inioStudyDecorate.class
    F:EclipseWorkspaceJava300inioStudyDecorateStudy1.class
    F:EclipseWorkspaceJava300inioStudyDirStudy.class
    F:EclipseWorkspaceJava300inioStudyDrink.class
    F:EclipseWorkspaceJava300inioStudyIOstudy1.class
    F:EclipseWorkspaceJava300inioStudyIOstudy2.class
    F:EclipseWorkspaceJava300inioStudyIOstudy3.class
    F:EclipseWorkspaceJava300inioStudyIOstudy4.class
    F:EclipseWorkspaceJava300inioStudyIOstudy5.class
    F:EclipseWorkspaceJava300inioStudyIOstudy6.class
    F:EclipseWorkspaceJava300inioStudyIOstudy7.class
    F:EclipseWorkspaceJava300inioStudyMilk.class
    F:EclipseWorkspaceJava300inioStudySugar.class
    F:EclipseWorkspaceJava300in	eststringTestArrayCopy1.class
    F:EclipseWorkspaceJava300in	eststringTestMyArrayList.class
    F:EclipseWorkspaceJava300in	eststringTestMyHashMap.class
    F:EclipseWorkspaceJava300in	eststringTestNode.class
    F:EclipseWorkspaceJava300in	eststringTestTestBubbleSort.class
    F:EclipseWorkspaceJava300in	eststringTestTestCollection.class
    F:EclipseWorkspaceJava300in	eststringTestTestString.class
    F:EclipseWorkspaceJava300commons-io-2.6-sources.jar
    F:EclipseWorkspaceJava300commons-io-2.6.jar
    F:EclipseWorkspaceJava300order.txt
    F:EclipseWorkspaceJava300output.txt
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest1.java
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest2.java
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest3.java
    F:EclipseWorkspaceJava300srcioStudyCopy.java
    F:EclipseWorkspaceJava300srcioStudyDecorateStudy1.java
    F:EclipseWorkspaceJava300srcioStudyDirStudy.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy1.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy2.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy3.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy4.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy5.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy6.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy7.java
    F:EclipseWorkspaceJava300src	eststringTestArrayCopy1.java
    F:EclipseWorkspaceJava300src	eststringTestMyArrayList.java
    F:EclipseWorkspaceJava300src	eststringTestMyHashMap.java
    F:EclipseWorkspaceJava300src	eststringTestTestBubbleSort.java
    F:EclipseWorkspaceJava300src	eststringTestTestCollection.java
    F:EclipseWorkspaceJava300src	eststringTestTestString.java
    F:EclipseWorkspaceJava300	empa.jpg
    F:EclipseWorkspaceJava300	est.txt
    F:EclipseWorkspaceJava300	estcopy.txt
    -----------------------------
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest1.java
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest2.java
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest3.java
    F:EclipseWorkspaceJava300srcioStudyCopy.java
    F:EclipseWorkspaceJava300srcioStudyDecorateStudy1.java
    F:EclipseWorkspaceJava300srcioStudyDirStudy.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy1.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy2.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy3.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy4.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy5.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy6.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy7.java
    F:EclipseWorkspaceJava300src	eststringTestArrayCopy1.java
    F:EclipseWorkspaceJava300src	eststringTestMyArrayList.java
    F:EclipseWorkspaceJava300src	eststringTestMyHashMap.java
    F:EclipseWorkspaceJava300src	eststringTestTestBubbleSort.java
    F:EclipseWorkspaceJava300src	eststringTestTestCollection.java
    F:EclipseWorkspaceJava300src	eststringTestTestString.java
    -----------------------------
    F:EclipseWorkspaceJava300order.txt
    F:EclipseWorkspaceJava300output.txt
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest1.java
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest2.java
    F:EclipseWorkspaceJava300srccommosIOCommosIOTest3.java
    F:EclipseWorkspaceJava300srcioStudyCopy.java
    F:EclipseWorkspaceJava300srcioStudyDecorateStudy1.java
    F:EclipseWorkspaceJava300srcioStudyDirStudy.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy1.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy2.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy3.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy4.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy5.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy6.java
    F:EclipseWorkspaceJava300srcioStudyIOstudy7.java
    F:EclipseWorkspaceJava300src	eststringTestArrayCopy1.java
    F:EclipseWorkspaceJava300src	eststringTestMyArrayList.java
    F:EclipseWorkspaceJava300src	eststringTestMyHashMap.java
    F:EclipseWorkspaceJava300src	eststringTestTestBubbleSort.java
    F:EclipseWorkspaceJava300src	eststringTestTestCollection.java
    F:EclipseWorkspaceJava300src	eststringTestTestString.java
    F:EclipseWorkspaceJava300	est.txt
    F:EclipseWorkspaceJava300	estcopy.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"), "锄禾日当午 ","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 }
  • 相关阅读:
    我从0开始开发了一个LDAP服务。
    C#开发中常用的小功能
    webapi swagger 报错 路由集合中已存在名为“swagger_docsswagger/docs/{apiVersion}”的路由。路由名称必须唯一
    h5 web vlc 播放rtsp流
    Docker的基础概念与在window10下的安装
    .Net Core JWT 动态设置接口与权限
    .Net Core官方的 JWT 授权验证
    IdentityServer4中文文档
    中介者模式及在NetCore中的使用MediatR来实现
    .Net Core 使用 FluentValidation
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10978745.html
Copyright © 2020-2023  润新知