
- 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
廣告