1

Looking to verify that there's no case-sensitive methods for processing file/path references.

This is for a use like looking up a file with a ".pdf" and/or ".PDF" extension in the directory without knowing whether it is upper- or lower-case.

java.nio.file.Files doesn't seem to have it. the only way I can think of is checking both cases or using String.equalsIgnoreCase() at it.

is there a better way of doing this?

  • Maybe `FileUtils.listFiles(theDir, new String[]{"pdf", "PDF" }, ...)` with `FileUtils` from [apache-commons](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)) ? –  Sep 21 '16 at 16:32
  • @RC cant upvote your comment. the ans gets it. – user6762070 Sep 21 '16 at 16:49
  • So the question was "How to remove the extension from a filename?", according to the accepted answer.. –  Sep 21 '16 at 16:56

2 Answers2

0

you can use external jar like apache-commons , and read file without caring of extension type or case sensitivity

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

There also other ways will help you there :How to get the filename without the extension in Java?

Community
  • 1
  • 1
Ahmed Gamal
  • 1,666
  • 1
  • 17
  • 25
0

You could use a FileNameFilter, something like:

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

Alternatively, you could use Files::list:

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

You can then act on the stream or collect it into a list for example.

assylias
  • 321,522
  • 82
  • 660
  • 783