List of utility methods to do File System
Path | findFile(FileSystem fileSystem, String path, String optionalFileSuffix) find File Path file = fileSystem.getPath(path); if (!lenientExists(file)) { file = fileSystem.getPath(path + optionalFileSuffix); return file; |
FileSystem | getFileSystem(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; |
FileSystem | getFileSystem(Path path) get File System return FileSystems.getFileSystem(path.toUri());
|
Path | getPath(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)); |
boolean | isFileSystemAvailable(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); |
boolean | isFileSystemAvailable2(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); ... |
Map | loadSystemResourceKeyValueCsvFileToMap(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; ... |
FileSystem | newJarFileSystem(Path jarFilePath) new Jar File System return FileSystems.newFileSystem(URI.create("jar:" + jarFilePath.toUri().toString()), new HashMap<String, Object>()); |
OutputStream | newOutputStream(FileSystem system, Path path) new Output Stream FileSystemProvider provider = system.provider();
return provider.newOutputStream(path);
|
List | systemEnvironmentPaths() system Environment Paths List<String> pathStrings = systemEnvironmentPathsAsStrings();
return pathStrings.stream().map(s -> Paths.get(s)).collect(Collectors.toList());
|