.apache.commons.io 源代码学习(一)

  java的初学者,准备通读各种高水平源代码,提升能力。

  为了避免自己的惰性,写博客。

  版本:2.5

  开发平台:netbeans。

 今天是第一天,网上先看个例子:http://www.importnew.com/13715.html 根据例子,去深入到源代码。

 org.apache.commons.io包中有很多工具类,里面多数类都是完成文件操作以及字符串比较的功能,下面列举了一下常用的工具类: FilenameUtils 这个工具类是用来处理文件名(译者注:包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题(比如windows和Unix)(在Unix系统以及Linux系统中文件分隔符是“/”,不支持”“,windows中支持”“以及”/“)。

 FileUtils提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。  IOCase提供字符串操作以及比较的方法。
 FileSystemUtils:提供查看指定目录剩余空间的方法。
 
 
import java.io.File;
import java.io.IOException;
 
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.IOCase;
 
public final class UtilityExample {
 
    // We are using the file exampleTxt.txt in the folder ExampleFolder,
    // and we need to provide the full path to the Utility classes.
    private static final String EXAMPLE_TXT_PATH =
            "C:UsersLilykosworkspaceApacheCommonsExampleExampleFolderexampleTxt.txt";
 
    private static final String PARENT_DIR =
            "C:UsersLilykosworkspaceApacheCommonsExample";
 
    public static void runExample() throws IOException {
        System.out.println("Utility Classes example...");
 
        // FilenameUtils
 
        System.out.println("Full path of exampleTxt: " +
                FilenameUtils.getFullPath(EXAMPLE_TXT_PATH));
 
        System.out.println("Full name of exampleTxt: " +
                FilenameUtils.getName(EXAMPLE_TXT_PATH));
 
        System.out.println("Extension of exampleTxt: " +
                FilenameUtils.getExtension(EXAMPLE_TXT_PATH));
 
        System.out.println("Base name of exampleTxt: " +
                FilenameUtils.getBaseName(EXAMPLE_TXT_PATH));
 
        // FileUtils
 
        // We can create a new File object using FileUtils.getFile(String)
        // and then use this object to get information from the file.
        File exampleFile = FileUtils.getFile(EXAMPLE_TXT_PATH);
        LineIterator iter = FileUtils.lineIterator(exampleFile);
 
        System.out.println("Contents of exampleTxt...");
        while (iter.hasNext()) {
            System.out.println("t" + iter.next());
        }
        iter.close();
 
        // We can check if a file exists somewhere inside a certain directory.
        File parent = FileUtils.getFile(PARENT_DIR);
        System.out.println("Parent directory contains exampleTxt file: " +
                FileUtils.directoryContains(parent, exampleFile));
 
        // IOCase
 
        String str1 = "This is a new String.";
        String str2 = "This is another new String, yes!";
 
        System.out.println("Ends with string (case sensitive): " +
                IOCase.SENSITIVE.checkEndsWith(str1, "string."));
        System.out.println("Ends with string (case insensitive): " +
                IOCase.INSENSITIVE.checkEndsWith(str1, "string."));
 
        System.out.println("String equality: " +
                IOCase.SENSITIVE.checkEquals(str1, str2));
 
        // FileSystemUtils
        System.out.println("Free disk space (in KB): " + FileSystemUtils.freeSpaceKb("C:"));
        System.out.println("Free disk space (in MB): " + FileSystemUtils.freeSpaceKb("C:") / 1024);
    }
}
View Code

下一次 就分析 FilenameUtils类。

  

原文地址:https://www.cnblogs.com/aomi/p/6433050.html