import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class PropertiesFileList {
public static void main(String[] args) {
// クラスパスを基準としたディレクトリのパス
String directoryPath = "/directory"; // ディレクトリが直下にある場合
// クラスローダーを使用してリソースを取得
ClassLoader classLoader = PropertiesFileList.class.getClassLoader();
URL resource = classLoader.getResource(directoryPath);
if (resource != null) {
// try-with-resourcesを使用してファイルリソースを自動的にクローズ
try (var directoryStream = new File(resource.getFile()).listFiles()) {
// 結果を格納するリスト
List<String> propertiesFiles = new ArrayList<>();
// ファイル一覧を走査して拡張子が.propertiesのファイルを抽出
if (directoryStream != null) {
for (File file : directoryStream) {
if (file.isFile() && file.getName().endsWith(".properties")) {
propertiesFiles.add(file.getName());
}
}
}
// 結果を出力
for (String fileName : propertiesFiles) {
System.out.println(fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("ディレクトリが見つかりませんでした。");
}
}
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme