Java (三)APACHE Commons IO 常规操作

上一篇:Java (二)基于Eclipse配置Commons IO的环境

 

例1:查看文件、文件夹的长度(大小)。

 1 import java.io.File;
 2 
 3 import org.apache.commons.io.FileUtils;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         long length = FileUtils.sizeOf(new File("J:/PrtSc/20190321/1.png"));
 9         System.out.println(length);
10         
11         length = FileUtils.sizeOf(new File("J:/PrtSc/20190321"));
12         System.out.println(length);
13         
14         length = FileUtils.sizeOfDirectory(new File("J:/PrtSc/20190321"));
15         System.out.println(length);
16     }
17 }

控制台的输出:

23415
1015221
1015221

 

 

例2:列出指定目录下的非空文件。

 1 import java.io.File;
 2 import java.util.Collection;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.io.filefilter.EmptyFileFilter;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
10         System.out.println(System.getProperty("user.dir") + "\n");
11         
12         Collection<File> files1 = FileUtils.listFiles(new File(System.getProperty("user.dir")), EmptyFileFilter.NOT_EMPTY, null);
13         for (File file: files1) {
14             System.out.println(file.getAbsolutePath());
15         }
16         
17         System.out.println("\n" + new File("empty.txt").length() + " bytes");
18     }
19 }

因为empty.txt是一个空文件,所以不会被列出来。

控制台的输出:

I:\Java\mycode\CommonsIO

I:\Java\mycode\CommonsIO\.classpath
I:\Java\mycode\CommonsIO\.project

0 bytes

 

如果向empty.txt写入内容,再次运行程序,empty.txt就会被列出。因为此时empty.txt不是一个空文件。

控制台的输出:

I:\Java\mycode\CommonsIO

I:\Java\mycode\CommonsIO\.classpath
I:\Java\mycode\CommonsIO\.project
I:\Java\mycode\CommonsIO\empty.txt

47 bytes

 

 

例3:列出指定目录,以及其子目录下的非空文件。

 1 import java.io.File;
 2 import java.util.Collection;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.io.filefilter.DirectoryFileFilter;
 6 import org.apache.commons.io.filefilter.EmptyFileFilter;
 7 
 8 public class Main {
 9 
10     public static void main(String[] args) {
11         System.out.println(System.getProperty("user.dir") + "\n");
12         
13         Collection<File> files1 = FileUtils.listFiles(new File(System.getProperty("user.dir")), EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);
14         for (File file: files1) {
15             System.out.println(file.getAbsolutePath());
16         }
17     }
18 }

控制台的输出:

I:\Java\mycode\CommonsIO

I:\Java\mycode\CommonsIO\.classpath
I:\Java\mycode\CommonsIO\.project
I:\Java\mycode\CommonsIO\.settings\org.eclipse.jdt.core.prefs
I:\Java\mycode\CommonsIO\bin\com\mk\Main.class
I:\Java\mycode\CommonsIO\lib\commons-io-2.6-sources.jar
I:\Java\mycode\CommonsIO\lib\commons-io-2.6.jar
I:\Java\mycode\CommonsIO\src\com\mk\Main.java

  

 

例4.1:列出指定目录下,指定后缀的文件。

 1 import java.io.File;
 2 import java.util.Collection;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.io.filefilter.DirectoryFileFilter;
 6 import org.apache.commons.io.filefilter.SuffixFileFilter;
 7 
 8 public class Main {
 9 
10     public static void main(String[] args) {
11         System.out.println(System.getProperty("user.dir") + "\n");
12         
13         Collection<File> files1 = FileUtils.listFiles(new File(System.getProperty("user.dir")), new SuffixFileFilter("java"), DirectoryFileFilter.INSTANCE);
14         for (File file: files1) {
15             System.out.println(file.getAbsolutePath());
16         }
17     }
18 }

控制台的输出:

I:\Java\mycode\CommonsIO

I:\Java\mycode\CommonsIO\src\com\mk\Main.java

  

 

例4.2:列出指定目录下,指定后缀的文件。

 1 import java.io.File;
 2 import java.util.Collection;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.io.filefilter.DirectoryFileFilter;
 6 import org.apache.commons.io.filefilter.FileFilterUtils;
 7 import org.apache.commons.io.filefilter.SuffixFileFilter;
 8 
 9 public class Main {
10 
11     public static void main(String[] args) {
12         System.out.println(System.getProperty("user.dir") + "\n");
13         
14         Collection<File> files1 = FileUtils.listFiles(new File(System.getProperty("user.dir")), FileFilterUtils.or(new SuffixFileFilter("java"), new SuffixFileFilter("class")), DirectoryFileFilter.INSTANCE);
15         for (File file: files1) {
16             System.out.println(file.getAbsolutePath());
17         }
18     }
19 }

控制台的输出:

I:\Java\mycode\CommonsIO

I:\Java\mycode\CommonsIO\bin\com\mk\Main.class
I:\Java\mycode\CommonsIO\src\com\mk\Main.java

 

 

例4.3:列出指定目录下,指定后缀的文件。

 1 import java.io.File;
 2 import java.util.Collection;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.io.filefilter.DirectoryFileFilter;
 6 import org.apache.commons.io.filefilter.EmptyFileFilter;
 7 import org.apache.commons.io.filefilter.FileFilterUtils;
 8 import org.apache.commons.io.filefilter.SuffixFileFilter;
 9 
10 public class Main {
11 
12     public static void main(String[] args) {
13         System.out.println(System.getProperty("user.dir") + "\n");
14         
15         Collection<File> files1 = FileUtils.listFiles(new File(System.getProperty("user.dir")), FileFilterUtils.or(new SuffixFileFilter("java"), new SuffixFileFilter("class"), EmptyFileFilter.EMPTY), DirectoryFileFilter.INSTANCE);
16         for (File file: files1) {
17             System.out.println(file.getAbsolutePath());
18         }
19     }
20 }

控制台的输出:

I:\Java\mycode\CommonsIO

I:\Java\mycode\CommonsIO\bin\com\mk\Main.class
I:\Java\mycode\CommonsIO\empty.txt
I:\Java\mycode\CommonsIO\src\com\mk\Main.java

 

 

例5.1:读取文本文档的内容。

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) throws IOException {
 9         System.out.println(System.getProperty("user.dir") + "\n");
10         
11         String message = FileUtils.readFileToString(new File("empty.txt"), "GBK");
12         System.out.println(message);
13         System.out.println();
14         
15         byte[] bytes = FileUtils.readFileToByteArray(new File("empty.txt"));
16         System.out.println(bytes.length + " bytes");
17         System.out.println(new String(bytes));
18     }
19 }

控制台的输出:

I:\Java\mycode\CommonsIO

锄禾日当午
码农真辛苦
一段小字符
一写一下午

46 bytes
锄禾日当午
码农真辛苦
一段小字符
一写一下午

 

 

例5.2:逐行读取文本文档的内容。

 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.util.List;
 4 
 5 import org.apache.commons.io.FileUtils;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) throws IOException {
10         System.out.println(System.getProperty("user.dir") + "\n");
11         
12         List<String> strings = FileUtils.readLines(new File("empty.txt"), "GBK");
13         for (String str: strings) {
14             System.out.println(str);
15         }
16     }
17 }

控制台的输出:

I:\Java\mycode\CommonsIO

锄禾日当午
码农真辛苦
一段小字符
一写一下午

 

 

例5.3:逐行读取文本文档的内容。

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.io.LineIterator;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) throws IOException {
10         System.out.println(System.getProperty("user.dir") + "\n");
11         
12         LineIterator lineIterator = FileUtils.lineIterator(new File("empty.txt"), "GBK");
13         while (lineIterator.hasNext()) {
14             System.out.println(lineIterator.nextLine());
15         }
16     }
17 }

控制台的输出:

I:\Java\mycode\CommonsIO

锄禾日当午
码农真辛苦
一段小字符
一写一下午

 

例6.1:写内容到文件。

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) throws IOException {
 9         System.out.println(System.getProperty("user.dir") + "\n");
10         
11         FileUtils.write(new File("text.txt"), "Where ", "GBK", true);
12         FileUtils.writeStringToFile(new File("text.txt"), "are ", "GBK", true);
13         FileUtils.writeByteArrayToFile(new File("text.txt"), "we?".getBytes("GBK"), true);
14     }
15 }

 

运行程序,在工程目录下生成一个文件text.txt,内容:

Where are we?

 

 

例6.2:写内容到文件。

 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.apache.commons.io.FileUtils;
 7 
 8 public class Main {
 9 
10     public static void main(String[] args) throws IOException {
11         System.out.println(System.getProperty("user.dir") + "\n");
12         
13         List<String> str = new ArrayList<String>();
14         str.add("Bruce Wayne: I've had training.");
15         str.add("Ducard: The training is nothing! Will is everything! The will to act.");
16         
17         FileUtils.writeLines(new File("text.txt"), str, "\r\n", false);
18     }
19 }

 

运行程序,在工程目录下生成一个文件text.txt,内容:

Bruce Wayne: I've had training.
Ducard: The training is nothing! Will is everything! The will to act.

 

下一篇:Java (四)APACHE Commons IO 复制文件

posted @ 2019-05-12 23:10  heismk  阅读(372)  评论(0编辑  收藏  举报