Java Utililty Methods File System

List of utility methods to do File System

Description

The list of methods to do File System are organized into topic(s).

Method

PathfindFile(FileSystem fileSystem, String path, String optionalFileSuffix)
find File
Path file = fileSystem.getPath(path);
if (!lenientExists(file)) {
    file = fileSystem.getPath(path + optionalFileSuffix);
return file;
FileSystemgetFileSystem(Path file)
get File System
FileSystem fs = null;
try {
    URI fileUri = file.toUri();
    URI uri = new URI("jar:" + fileUri.getScheme(), fileUri.getPath(), null);
    fs = FileSystems.newFileSystem(uri, new HashMap<String, String>());
} catch (Exception e) {
    e.printStackTrace();
return fs;
FileSystemgetFileSystem(Path path)
get File System
return FileSystems.getFileSystem(path.toUri());
PathgetPath(FileSystem targetFS, String fileName)
get Path
String[] nameComps = fileName.replace('\\', '/').split("/");
if (nameComps.length == 1) {
    return targetFS.getPath(nameComps[0]);
} else {
    return targetFS.getPath(nameComps[0], Arrays.copyOfRange(nameComps, 1, nameComps.length));
booleanisFileSystemAvailable(final Path file, final String topLevelAbsolutePath)
Determines via best effort if the file system on which the file resides is available.
if (isEqualPath(file, topLevelAbsolutePath)) {
    return Files.exists(file);
return isFileSystemAvailable2(file, topLevelAbsolutePath);
booleanisFileSystemAvailable2(final Path file, final String topLevelAbsolutePath)
is File System Available
boolean available;
if (Files.exists(file)) {
    available = true;
} else {
    if (isEqualPath(file, topLevelAbsolutePath)) {
        available = false;
    } else {
        available = isFileSystemAvailable2(file.getParent(), topLevelAbsolutePath);
...
MaploadSystemResourceKeyValueCsvFileToMap(String resourcePath)
Loads the given system resource and assumes it is a csv file with a key and value column.
URL testFileUrl = ClassLoader.getSystemResource(resourcePath);
URI testFileUri = testFileUrl.toURI();
Map<String, Double> results = new HashMap<>();
Files.lines(Paths.get(testFileUri)).forEach(x -> {
    String[] strings = x.split(",");
    results.put(strings[0], Double.parseDouble(strings[1]));
});
return results;
...
FileSystemnewJarFileSystem(Path jarFilePath)
new Jar File System
return FileSystems.newFileSystem(URI.create("jar:" + jarFilePath.toUri().toString()),
        new HashMap<String, Object>());
OutputStreamnewOutputStream(FileSystem system, Path path)
new Output Stream
FileSystemProvider provider = system.provider();
return provider.newOutputStream(path);
ListsystemEnvironmentPaths()
system Environment Paths
List<String> pathStrings = systemEnvironmentPathsAsStrings();
return pathStrings.stream().map(s -> Paths.get(s)).collect(Collectors.toList());