Package com.opengamma.strata.collect.io
Class SafeFiles
- java.lang.Object
-
- com.opengamma.strata.collect.io.SafeFiles
-
public final class SafeFiles extends Object
Provides methods to operate on files usingPath
that avoid leaking file handles.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
lines(Path file, Function<Stream<String>,T> function)
Streams the lines in the specified file using UTF-8.static List<String>
linesAll(Path file)
Loads the specified file as a list of lines using UTF-8.static <T> T
list(Path dir, Function<Stream<Path>,T> function)
Streams the elements in the specified directory without recursing into subdirectories.static List<Path>
listAll(Path dir)
Lists the elements in the specified directory without recursing into subdirectories.static <T> T
walk(Path dir, int maxDepth, Function<Stream<Path>,T> function, FileVisitOption... options)
Lists the elements in the specified directory, recursing depth-first into subdirectories.static List<Path>
walkAll(Path dir)
Lists the elements in the specified directory, recursing depth-first into subdirectories.static List<Path>
walkAll(Path dir, int maxDepth, FileVisitOption... options)
Lists the elements in the specified directory, recursing depth-first into subdirectories.
-
-
-
Method Detail
-
listAll
public static List<Path> listAll(Path dir)
Lists the elements in the specified directory without recursing into subdirectories.This is a safe wrapper for
Files.list(Path)
.- Parameters:
dir
- the path to list files- Returns:
- the list of paths in the specified directory
- Throws:
SecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
list
public static <T> T list(Path dir, Function<Stream<Path>,T> function)
Streams the elements in the specified directory without recursing into subdirectories.This is a safe wrapper for
Files.list(Path)
.- Type Parameters:
T
- the type of the result- Parameters:
dir
- the path to list filesfunction
- the function to apply to the stream- Returns:
- the result of the function
- Throws:
SecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
walkAll
public static List<Path> walkAll(Path dir)
Lists the elements in the specified directory, recursing depth-first into subdirectories.This is a safe wrapper for
Files.walk(Path, FileVisitOption...)
. It recurses into all subdirectories and does not follow links.- Parameters:
dir
- the start path to list files from- Returns:
- the list of paths in the specified directory
- Throws:
IllegalArgumentException
- if maxDepth is negativeSecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
walkAll
public static List<Path> walkAll(Path dir, int maxDepth, FileVisitOption... options)
Lists the elements in the specified directory, recursing depth-first into subdirectories.This is a safe wrapper for
Files.walk(Path, int, FileVisitOption...)
.- Parameters:
dir
- the start path to list files frommaxDepth
- the maximum depth to recurse to, one for one-level and so onoptions
- the options- Returns:
- the list of paths in the specified directory
- Throws:
IllegalArgumentException
- if maxDepth is negativeSecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
walk
public static <T> T walk(Path dir, int maxDepth, Function<Stream<Path>,T> function, FileVisitOption... options)
Lists the elements in the specified directory, recursing depth-first into subdirectories.This is a safe wrapper for
Files.walk(Path, int, FileVisitOption...)
.- Type Parameters:
T
- the type of the result- Parameters:
dir
- the start path to list files frommaxDepth
- the maximum depth to recurse to, one for one-level and so onfunction
- the function to apply to the streamoptions
- the options- Returns:
- the result of the function
- Throws:
IllegalArgumentException
- if maxDepth is negativeSecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
linesAll
public static List<String> linesAll(Path file)
Loads the specified file as a list of lines using UTF-8.This is a safe wrapper for
Files.lines(Path)
.- Parameters:
file
- the path to read- Returns:
- the list of lines in the specified file
- Throws:
SecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
lines
public static <T> T lines(Path file, Function<Stream<String>,T> function)
Streams the lines in the specified file using UTF-8.This is a safe wrapper for
Files.lines(Path)
.- Type Parameters:
T
- the type of the result- Parameters:
file
- the path to readfunction
- the function to apply to the stream- Returns:
- the result of the function
- Throws:
SecurityException
- if access is rejected by a security managerUncheckedIOException
- if an IO error occurs
-
-