- Apache Commons IO 教程
- Apache Commons IO - 主页
- Apache Commons IO - 概览
- Apache Commons IO - 环境设置
- Apache Commons IO - IOUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - IOCase
- Apache Commons IO - LineIterator
- Apache Commons IO - NameFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - AndFileFilter
- Apache Commons IO - FileEntry
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - NameFileComparator
- Apache Commons IO - SizeFileComparator
- LastModifiedFileComparator
- Apache Commons IO - TeeInputStream
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - 有用资源
- Apache Commons IO - 快速指南
- Apache Commons IO - 有用资源
- Apache Commons IO - 讨论
Apache Commons IO - FilenameUtils
提供无需使用 File 对象便可处理文件名的方法。它在不同操作系统中以相似的方式工作。此类在从基于 Windows 的开发计算机迁移到基于 Unix 的生产计算机时解决了问题。
类声明
下面是 org.apache.commons.io.FilenameUtils 类的声明 -
public class FilenameUtils extends Object
FilenameUtils 的特性
此类在文件名中定义六个部分。以 C:\dev\project\file.txt 为例。各部分为 -
- 前缀 - C:\
- 相对路径 - dev\project\
- 绝对路径 - C:\dev\project\
- 名称 - file.txt
- 基名 - file
- 扩展名 - txt
要识别目录,请在文件名中添加分隔符。
FilenameUtils 类的示例
下面是 FilenameUtils 类的示例 -
IOTester.java
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;
public class IOTester {
public static void main(String[] args) {
try {
//Using FilenameUtils
usingFilenameUtils();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public static void usingFilenameUtils() throws IOException {
String path = "C:\\dev\\project\\file.txt";
System.out.println("Full Path: " +FilenameUtils.getFullPath(path));
System.out.println("Relative Path: " +FilenameUtils.getPath(path));
System.out.println("Prefix: " +FilenameUtils.getPrefix(path));
System.out.println("Extension: " + FilenameUtils.getExtension(path));
System.out.println("Base: " + FilenameUtils.getBaseName(path));
System.out.println("Name: " + FilenameUtils.getName(path));
String filename = "C:/commons/io/../lang/project.xml";
System.out.println("Normalized Path: " + FilenameUtils.normalize(filename));
}
}
输出
它将打印以下结果。
Full Path: C:\dev\project\ Relative Path: dev\project\ Prefix: C:\ Extension: txt Base: file Name: file.txt Normalized Path: C:\commons\lang\project.xml
广告