0
0

Pathの拡張子をチェックしたい

Posted at

Pathの拡張子をチェックしたい場合、PathMatcherクラスのmatchesメソッドを利用します。

例) Pathの拡張子がcsvかどうかをチェックする

Path path = Path.of("Path/To/Target.csv");
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.csv");
if (matcher.matches(path)) {
	System.out.println("This file is csv.");
} else {
	System.out.println("This file is not csv.");
}

環境情報

C:\>javac -version
javac 21-beta

C:\>java -version
openjdk version "21-beta" 2023-09-19
OpenJDK Runtime Environment Temurin-21+35-202309042131 (build 21-beta+35-ea)
OpenJDK 64-Bit Server VM Temurin-21+35-202309042131 (build 21-beta+35-ea, mixed mode, sharing)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0