Apache Commons IO教程:初学者指南

Apache Commons IO是由Apache Foundation创建和维护的Java库。 它提供了许多类,使开发人员可以轻松地完成常见任务,并且减少样板代码 ,而每个项目都需要一遍又一遍地编写此类库的重要性是巨大的,因为它们已经成熟由经验丰富的开发人员进行维护 ,他们已经考虑了每种可能的情况,或者修复了各种错误。

在此示例中,我们将根据功能所属的org.apache.commons.io包介绍一些具有不同功能的方法。 我们不会在库中深入研究,因为它巨大,但是我们将提供一些常见用法的示例,这些示例对于每个开发人员(无论初学者或不入门)都可以派上用场。

1. Apache Commons IO示例

该示例的代码将分为几个类,并且每个类都代表Apache Commons IO涵盖的特定领域。 这些区域是:

  • 实用程序类
  • 输入项
  • 输出量
  • 筛选器
  • 比较器
  • 文件监控

为了使事情更清楚,我们将输出分成多个 ,每个创建的类一个。 我们还在项目文件夹(名为ExampleFolder )内创建了一个目录,其中包含将在此示例中使用的各种文件,以显示各种类的功能。

注意:为了使用org.apache.commons.io ,您需要下载jar文件(在此处找到),并通过右键单击项目文件夹-> Build Path->将它们添加到Eclipse项目的构建路径中。添加外部档案。

ApacheCommonsExampleMain.java

public class ApacheCommonsExampleMain {

    public static void main(String[] args) {
        UtilityExample.runExample();
        
        FileMonitorExample.runExample();
        
        FiltersExample.runExample();
        
        InputExample.runExample();
        
        OutputExample.runExample();
        
        ComparatorExample.runExample();
    }
}

这是将用于运行示例中其他类的方法的主要类。 您可以注释某些类以查看所需的输出。

1.1实用程序类

org.apache.commons.io内有各种实用程序类,其中大多数与文件操作和字符串比较有关。 我们在这里使用了一些最重要的方法:

  • FilenameUtils :此类具有使用文件名的方法,主要要点是使每个OS的工作更轻松(在Unix和Windows系统中同样有效)。
  • FileUtils :它提供用于文件操作 (移动,打开和读取文件,检查文件是否存在等)的方法。
  • IOCase :字符串操作和比较方法。
  • FileSystemUtils :其方法返回指定驱动器的可用空间。

UtilityExample.java

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:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleTxt.txt";
    
    private static final String PARENT_DIR =
            "C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample";

    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.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值