2016-09-21 19 views
1

尋求驗證是否沒有區分大小寫的方法來處理文件/路徑引用。區分大小寫的文件名處理

這是爲了像在目錄中查找帶有「.pdf」和/或「.PDF」擴展名的文件而不知道它是大寫還是小寫的用法。

java.nio.file.Files似乎沒有它。我能想到的唯一方法是檢查兩種情況或使用String.equalsIgnoreCase()

有沒有更好的方法來做到這一點?

+0

也許''FileUtils.listFiles(theDir,new String [] {「pdf」,「PDF」},...)''來自[apache-commons]的'FileUtils'(https://commons.apache.org /proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#listFiles(java.io.File,%20java.lang.String[],%20boolean))? – 2016-09-21 16:32:47

+0

@RC不能upvote您的評論。該ans得到它。 – user6762070

+0

所以問題是「如何從文件名中刪除擴展名?」,根據接受的答案.. – 2016-09-21 16:56:23

回答

0

您可以使用外部的jar像Apache的公地,以及不帶擴展型或區分大小寫的關懷讀取文件

import org.apache.commons.io.FilenameUtils; 
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt); 

還有其他的方法可以幫助你在那裏:How to get the filename without the extension in Java?

0

你可以使用a FileNameFilter ,是這樣的:

File folder = Paths.get("/path/to/folder").toFile(); 
String[] pdfs = folder.list((dir, name) -> name.endsWith(".pdf") || name.endsWith(".PDF")); 

或者,你可以使用Files::list

Files.list(Paths.get("c:/temp")) 
    .filter(p -> p.getFileName().toString().endsWith(".pdf") || 
        p.getFileName().toString().endsWith(".PDF")) 

然後,您就可以對流進行操作或將其收集到列表中。

相關問題