Package com.mastfrog.util.file
Class FileUtils
java.lang.Object
com.mastfrog.util.file.FileUtils
Utility methods for reading and writing files and directories. The read and
write methods take special care to be memory-efficient and avoid unnecessary
memory-copies.
- Author:
- Tim Boudreau
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbyExtension(String ext) Get a predicate for filtering file streams which only lets non-folders with a give file extension delimited by.through.static int[]copyFolderTree(Path from, Path to) Copy an entire folder tree.static intdecode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, boolean permissive) static intdecode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, boolean permissive, CoderResult[] res) static intdecode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, Obj<CoderResult> res, boolean permissive) static booleandeleteIfExists(Path path) For use mainly by tests - delete a file if it exists, swallowing any NoSuchFileException or FileNotFoundException that might be caused by a race condition; accepts null as an argument and returns false.static booleandeleteIfExists(Path first, Path... more) For use mainly by tests - delete files if they exist, swallowing any NoSuchFileException or FileNotFoundException that might be caused by a race condition; accepts null as an argument and returns false.static voidDelete a folder and its subtree; handles the condition that this method may race and files may already have been deleted (for example, shutting down a database process which deletes its pidfile) after the set of files being iterated was computed, silently.Get a predicate for filtering file streams which only lets files through.filesToPaths(Iterable<File> file) For adaptering old and new file apis, convert an iterator of files to an iterable of paths.filesToPaths(Iterator<File> files) For adaptering old and new file apis, convert an iterator of files to an iterator of paths.Find all files in the subtree under a folder which have the given file extension.Find all files in the subtree under a folder which have the given file extension.static PathfindExecutable(String command) Find an executable file with the passed file name, search the system path and the default search path (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin).static PathfindExecutable(String command, boolean useDefaultSearchPath, boolean useSystemPath, String... alsoSearch) Find an executable file with the passed file name, search the system path and the default search path (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin).static PathfindExecutable(String command, String... alsoSearch) Find an executable file with the passed file name, search the system path and the default search path (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin).Get a predicate for filtering file streams which only lets folders through.static Pathhome()static StringhomeRelativePath(Path what) Returns a shorter string prefixed with ~/ if the passed path is below the user's home dir.ifDirectory(Path path) static ThrowingOptional<Path>ifDirectoryT(Path path) static ThrowingOptional<Path>static Stream<CharSequence>Get a stream which lazily loads each line of a file as UTF-8 (unlike Files.readAllLines()) allowing very large files to be parsed line by line without iteration necessarily requiring more than(DEFAULT_BUFFER_SIZE * 2) + (maxLineLength * charset-max-bytes-per-char)bytes of memory.static Stream<CharSequence>Get a stream which lazily loads each line of a file (unlike Files.readAllLines()) allowing very large files to be parsed line by line without iteration necessarily requiring more than(bufferSize * 2) + (maxLineLength * charset-max-bytes-per-char)bytes of memory.static Stream<CharSequence>Get a stream which lazily loads each line of a file (unlike Files.readAllLines()) allowing very large files to be parsed line by line without iteration necessarily requiring more than(bufferSize * 2) + (maxLineLength * charset-max-bytes-per-char)bytes of memory.Create a new file with the given name and extension, appending "-1", "-2", and so forth to the name portion until a name is generated which does not already exist.Create a path to a nonexistent file of the given name and extension, appending "-1", "-2", and so forth to the name portion until a name is generated which does not already exist.static PathCreate a new temporary subfolder in the system temporary files folder.static PathnewTempDir(String prefix) Create a new temporary subfolder in the system temporary files folder.static PathnewTempDir(String prefix, PosixFilePermission... permissions) Create a new temporary subfolder in the system temporary files folder.static PathCreate a new temporary file on disk in the system temporary files folder, with default permissons.static PathnewTempFile(String prefix) Create a new temporary file in the system temporary files folder.static PathnewTempFile(String prefix, PosixFilePermission... permissions) Create a new temporary file in the system temporary files folder.static PathnewTempFile(PosixFilePermission... perms) Create a new temporary file on disk in the system temporary files folder.static PathnewTempPath(String prefix) Create a new file or directory path in the system temporary directory without creating anything on disk.pathsToFiles(Iterable<Path> file) For adaptering old and new file apis, convert an iterator of paths to an iterable of files.pathsToFiles(Iterator<Path> paths) For adaptering old and new file apis, convert an iterator of paths to an iterator of files.static CharSequenceRead an ASCII character sequence from a file using default settings and permissive handling of coding errors.static StringreadAsciiString(Path path) Read an ASCII character sequence from a file using default settings and permissive handling of coding errors.static CharSequencereadCharSequence(boolean directBuffers, int bufferSize, Charset charset, boolean permissive, ReadableByteChannel[] channelHolder, IOSupplier<ReadableByteChannel> ch) Read a character sequence from a channel at its current position as supplied, using the passed settings to tune performance versus memory footprint and copying overhead; the returned character sequence wraps the decoded character buffers to minimize copies.static CharSequencereadCharSequence(Path path, boolean directBuffers, int bufferSize, Charset charset, boolean permissive) Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.static CharSequencereadCharSequence(Path path, int bufferSize, Charset charset) Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.static CharSequencereadCharSequence(Path path, int bufferSize, Charset charset, boolean permissive) Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.static CharSequencereadCharSequence(Path path, Charset charset) Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.static StringreadString(Path path, int bufferSize, Charset charset) Read a String from a file using default settings and permissive handling of coding errors.static StringreadString(Path path, int bufferSize, Charset charset, boolean permissive) Read a String from a file using default settings and permissive handling of coding errors.static StringreadString(Path path, Charset as) Read a String from a file using default settings and permissive handling of coding errors.static CharSequenceRead a UTF-8 character sequence from a file using default settings and permissive handling of coding errors.static StringreadUTF8String(Path path) Read a UTF-8 character sequence from a file using default settings and permissive handling of coding errors.static intsearch(boolean relativize, Path dir, boolean followLinks, Predicate<? super Path> predicate, Consumer<? super Path> consumer) Search the subtree of a directory for paths.static voidsetPermissions(Path path, PosixFilePermission... permissions) Convenience varargs version of Files.setPosixFilePermissions without having to either create an EnumSet, or create an intermediate ArrayList in order to do that.static IOFunction<Predicate<CharSequence>,Runnable> Tail a file, such as a log file.static IOFunction<Predicate<CharSequence>,Runnable> Tail a file, such as a log file.static Pathtemp()Get the system temporary directory.static voidunzip(InputStream in, Path dir) Unzip the passed input stream into the passed directory.static PathGet the cache directory in the user's home directory - on linux, ~/.cache, on Mac OS X ~/Library/Cachesstatic voidwriteAscii(Path path, CharSequence content) Write a character sequence to a file as ASCII.static <T extends WritableByteChannel>
TwriteCharSequence(CharSequence content, boolean permissive, Charset as, int bufferSize, boolean directBuffers, IOSupplier<? extends T> channelSupplier) Write a character sequence to a channel.static voidwriteFile(Path path, boolean permissive, CharSequence content, boolean directBuffers, Charset as, int bufferSize, boolean append, FileAttribute... attrs) Write a character sequence to a file.static voidwriteFile(Path path, boolean permissive, CharSequence content, Charset as, int bufferSize, boolean append, FileAttribute... attrs) Write a character sequence to a file.static voidwriteFile(Path path, CharSequence content, Charset as) Write a character sequence to a file.static voidwriteFile(Path path, CharSequence content, Charset as, boolean append, FileAttribute... attrs) Write a character sequence to a file.static voidwriteFile(Path path, CharSequence content, Charset as, int bufferSize, boolean append) Write a character sequence to a file.static voidwriteFile(Path path, CharSequence content, Charset as, int bufferSize, boolean append, FileAttribute... attrs) Write a character sequence to a file.static voidwriteUtf8(Path path, CharSequence content) Write a character sequence to a file as UTF-8.
-
Field Details
-
SYSPROP_DEFAULT_BUFFER_SIZE
- See Also:
-
-
Method Details
-
newFile
Create a new file with the given name and extension, appending "-1", "-2", and so forth to the name portion until a name is generated which does not already exist. Creates a 0-byte file.- Parameters:
dir- The foldername- The nameext- The extension- Returns:
- The created file
- Throws:
IOException
-
newPath
Create a path to a nonexistent file of the given name and extension, appending "-1", "-2", and so forth to the name portion until a name is generated which does not already exist.- Parameters:
dir- The foldername- The nameext- The extension- Returns:
- The created file
- Throws:
IOException
-
newTempDir
Create a new temporary subfolder in the system temporary files folder.- Returns:
- A new folder created on disk
- Throws:
IOException
-
newTempDir
Create a new temporary subfolder in the system temporary files folder.- Parameters:
prefix- String prefix to prepend to the file name for identification- Returns:
- A new folder created on disk
- Throws:
IOException- If something goes wrong
-
newTempDir
Create a new temporary subfolder in the system temporary files folder.- Parameters:
prefix- String prefix to prepend to the file name for identificationpermissions- The file permissions- Returns:
- A new folder created on disk
- Throws:
IOException- If something goes wrong
-
newTempFile
Create a new temporary file on disk in the system temporary files folder. An empty file is created on disk.- Parameters:
perms- File permissions - if empty, the default permissions are used.- Returns:
- A new file
- Throws:
IOException- If something goes wrong
-
newTempFile
Create a new temporary file on disk in the system temporary files folder, with default permissons. An empty file is created on disk.- Returns:
- A new file
- Throws:
IOException- If something goes wrong
-
newTempFile
Create a new temporary file in the system temporary files folder.- Parameters:
prefix- String prefix to prepend to the file name for identification- Returns:
- A new folder created on disk
- Throws:
IOException- If something goes wrong
-
newTempFile
public static Path newTempFile(String prefix, PosixFilePermission... permissions) throws IOException Create a new temporary file in the system temporary files folder.- Parameters:
prefix- String prefix to prepend to the file name for identificationpermissions- The file permissions- Returns:
- A new folder created on disk
- Throws:
IOException- If something goes wrong
-
setPermissions
Convenience varargs version of Files.setPosixFilePermissions without having to either create an EnumSet, or create an intermediate ArrayList in order to do that.Note: This method will not remove all permissions if it is passed an empty array - it will simply do nothing. To do that, use Files.setPosixFilePermissions(path, EnumSet.noneOf(PosixFilePermission.class).
- Parameters:
path- A pathpermissions- A set of permissions- Throws:
IOException
-
deleteIfExists
For use mainly by tests - delete files if they exist, swallowing any NoSuchFileException or FileNotFoundException that might be caused by a race condition; accepts null as an argument and returns false.- Parameters:
path- The file pathmore- More files to delete- Returns:
- True if any file was deleted
- Throws:
IOException- If something else goes wrong
-
deleteIfExists
For use mainly by tests - delete a file if it exists, swallowing any NoSuchFileException or FileNotFoundException that might be caused by a race condition; accepts null as an argument and returns false.- Parameters:
path- The file path- Returns:
- True if the file was deleted
- Throws:
IOException- If something else goes wrong
-
newTempPath
Create a new file or directory path in the system temporary directory without creating anything on disk.- Parameters:
prefix- The prefix to prepend to the file name- Returns:
- A file name
-
writeFile
public static void writeFile(Path path, CharSequence content, Charset as, int bufferSize, boolean append) throws IOException Write a character sequence to a file. This method provides tight control over how much memory is used to perform the write, and trade-offs between performance and memory pressure.- Parameters:
path- The file pathcontent- The character sequence to writeas- The character set to usebufferSize- The size of buffer to use, trading performance for memory use as desiredappend- If true, append to the file, rather than truncating it if it exists- Throws:
IOException- If something goes wrong
-
writeUtf8
Write a character sequence to a file as UTF-8.- Parameters:
path- The file pathcontent- The character sequence to write- Throws:
IOException- If something goes wrong
-
writeAscii
Write a character sequence to a file as ASCII.- Parameters:
path- The file pathpermissive- If true, do not throw exceptions if characters are unencodablecontent- The character sequence to writeas- The character set to use- Throws:
IOException- If something goes wrong
-
writeFile
Write a character sequence to a file. This method provides tight control over how much memory is used to perform the write, and trade-offs between performance and memory pressure.- Parameters:
path- The file pathpermissive- If true, do not throw exceptions if characters are unencodablecontent- The character sequence to writeas- The character set to use- Throws:
IOException- If something goes wrong
-
writeFile
public static void writeFile(Path path, CharSequence content, Charset as, boolean append, FileAttribute... attrs) throws IOException Write a character sequence to a file. This method provides tight control over how much memory is used to perform the write, and trade-offs between performance and memory pressure.- Parameters:
path- The file pathpermissive- If true, do not throw exceptions if characters are unencodablecontent- The character sequence to writeas- The character set to useappend- If true, append to the file, rather than truncating it if it existsattrs- Any file attributes to apply if the file is being created- Throws:
IOException- If something goes wrong
-
writeFile
public static void writeFile(Path path, CharSequence content, Charset as, int bufferSize, boolean append, FileAttribute... attrs) throws IOException Write a character sequence to a file. This method provides tight control over how much memory is used to perform the write, and trade-offs between performance and memory pressure.- Parameters:
path- The file pathpermissive- If true, do not throw exceptions if characters are unencodablecontent- The character sequence to writeas- The character set to usebufferSize- The size of buffer to use, trading performance for memory use as desiredappend- If true, append to the file, rather than truncating it if it existsattrs- Any file attributes to apply if the file is being created- Throws:
IOException- If something goes wrong
-
writeFile
public static void writeFile(Path path, boolean permissive, CharSequence content, Charset as, int bufferSize, boolean append, FileAttribute... attrs) throws IOException Write a character sequence to a file. This method provides tight control over how much memory is used to perform the write, and trade-offs between performance and memory pressure.- Parameters:
path- The file pathpermissive- If true, do not throw exceptions if characters are unencodablecontent- The character sequence to writeas- The character set to usebufferSize- The size of buffer to use, trading performance for memory use as desiredappend- If true, append to the file, rather than truncating it if it existsattrs- Any file attributes to apply if the file is being created- Throws:
IOException- If something goes wrong
-
writeFile
public static void writeFile(Path path, boolean permissive, CharSequence content, boolean directBuffers, Charset as, int bufferSize, boolean append, FileAttribute... attrs) throws IOException Write a character sequence to a file. This method provides tight control over how much memory is used to perform the write, and trade-offs between performance and memory pressure.- Parameters:
path- The file pathpermissive- If true, do not throw exceptions if characters are unencodablecontent- The character sequence to writedirectBuffers- Use direct buffers - slower performance for small files, but one less memory copyas- The character set to usebufferSize- The size of buffer to use, trading performance for memory use as desiredappend- If true, append to the file, rather than truncating it if it existsattrs- Any file attributes to apply if the file is being created- Throws:
IOException- If something goes wrong
-
writeCharSequence
public static <T extends WritableByteChannel> T writeCharSequence(CharSequence content, boolean permissive, Charset as, int bufferSize, boolean directBuffers, IOSupplier<? extends T> channelSupplier) throws IOException Write a character sequence to a channel. Note: this method does not close the channel (unless an exception is thrown while writing) - the channel is returned and may be closed by the caller if desired. This enables this method to be used with channel pools where the channel should not be closed.- Parameters:
content- The character sequence to writepermissive- If true, do not throw exceptions if characters are unencodableas- The character set to usebufferSize- The size of buffer to use, trading performance for memory use as desireddirectBuffers- Use direct buffers - slower performance for small files, but one less memory copychannelSupplier- The supplier which will open a writable channel- Returns:
- The channel which was written to
- Throws:
IOException- If something goes wrong
-
readAscii
Read an ASCII character sequence from a file using default settings and permissive handling of coding errors.- Parameters:
path- The file- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readAsciiString
Read an ASCII character sequence from a file using default settings and permissive handling of coding errors.- Parameters:
path- The file- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readUTF8
Read a UTF-8 character sequence from a file using default settings and permissive handling of coding errors.- Parameters:
path- The file- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readUTF8String
Read a UTF-8 character sequence from a file using default settings and permissive handling of coding errors.- Parameters:
path- The file- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readString
Read a String from a file using default settings and permissive handling of coding errors.- Parameters:
path- The fileas- The character set- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readCharSequence
Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.- Parameters:
path- The fileas- The character set- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readString
Read a String from a file using default settings and permissive handling of coding errors.- Parameters:
path- The filebufferSize- The size of the buffer used to read, trading memory for performance or performance for memory footprintas- The character set- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readCharSequence
public static CharSequence readCharSequence(Path path, int bufferSize, Charset charset) throws IOException Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.- Parameters:
path- The filebufferSize- The size of the buffer used to read, trading memory for performance or performance for memory footprintas- The character set- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readString
public static String readString(Path path, int bufferSize, Charset charset, boolean permissive) throws IOException Read a String from a file using default settings and permissive handling of coding errors.- Parameters:
path- The filebufferSize- The size of the buffer used to read, trading memory for performance or performance for memory footprintas- The character setpermissive- If true, ignore undecodable characters- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readCharSequence
public static CharSequence readCharSequence(Path path, int bufferSize, Charset charset, boolean permissive) throws IOException Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.- Parameters:
path- The filebufferSize- The size of the buffer used to read, trading memory for performance or performance for memory footprintas- The character setpermissive- If true, ignore undecodable characters- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readCharSequence
public static CharSequence readCharSequence(Path path, boolean directBuffers, int bufferSize, Charset charset, boolean permissive) throws IOException Read a character sequence from a file using default settings and permissive handling of coding errors; the returned character sequence wraps the character buffers decoded to minimize copies.- Parameters:
path- The filedirectBuffers- If true, use NIO direct buffers for reads, trading some performance for one less memory copybufferSize- The size of the buffer used to read, trading memory for performance or performance for memory footprintas- The character setpermissive- If true, ignore undecodable characters- Returns:
- A character sequence
- Throws:
IOException- If something goes wrong
-
readCharSequence
public static CharSequence readCharSequence(boolean directBuffers, int bufferSize, Charset charset, boolean permissive, ReadableByteChannel[] channelHolder, IOSupplier<ReadableByteChannel> ch) throws IOException Read a character sequence from a channel at its current position as supplied, using the passed settings to tune performance versus memory footprint and copying overhead; the returned character sequence wraps the decoded character buffers to minimize copies.- Parameters:
directBuffers- If true, use direct buffers to eliminate one memory copybufferSize- The number of bytes to read at a time to manage memory pressurecharset- The character setpermissive- If true, ignore encoding errorschannelHolder- The channel provided by the supplier is set as the zeroth element, to allow the caller to close the channel if it was created by the supplierch- A supplier of a channel; if the channel is already in existence, it is read from the current position; the channel will not be closed by this method unless an exception is thrown while reading- Returns:
- A character sequence which wraps the decoded character buffers
- Throws:
IOException- If something goes wrong
-
decode
public static int decode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, boolean permissive) throws IOException - Throws:
IOException
-
decode
public static int decode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, boolean permissive, CoderResult[] res) throws IOException - Throws:
IOException
-
decode
public static int decode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, Obj<CoderResult> res, boolean permissive) throws IOException - Throws:
IOException
-
deltree
Delete a folder and its subtree; handles the condition that this method may race and files may already have been deleted (for example, shutting down a database process which deletes its pidfile) after the set of files being iterated was computed, silently.- Parameters:
dir- A directory- Throws:
IOException- If a file cannot be deleted, the passed file is not a directory, or the process owner does not have the needed permissions
-
tail
public static IOFunction<Predicate<CharSequence>,Runnable> tail(Path file, Charset charset) throws IOException Tail a file, such as a log file. This method returns a function which takes a Predicate<CharSequence>, which will be called whenever a complete line of text is appended to the file (it will initially be passed all existing lines of the file). The function returns a Runnable which can be invoked to cancel tailing the file.Internally, uses ContinuousLineStream and the JDK's directory watch facility to unpause the thread that reads lines.
- Parameters:
file- The fail to tail (must exist)charset- The character set for lines- Returns:
- A function which takes a listener predicate (which may return false to abort watching) and returns a Runnable which can be invoked to cancel listening
- Throws:
IOException- If something goes wrong
-
tail
public static IOFunction<Predicate<CharSequence>,Runnable> tail(Path file, Charset charset, int bufferSize, Executor exe) throws IOException Tail a file, such as a log file. This method returns a function which takes a Predicate<CharSequence>, which will be called whenever a complete line of text is appended to the file (it will initially be passed all existing lines of the file). The function returns a Runnable which can be invoked to cancel tailing the file.Internally, uses ContinuousLineStream and the JDK's directory watch facility to unpause the thread that reads lines.
- Parameters:
file- The fail to tail (must exist)charset- The character set for linesbufferSize- The size of the internal buffer used to read bytes from diskexe- The thread pool the watch thread will run in- Returns:
- A function which takes a listener predicate (which may return false to abort watching) and returns a Runnable which can be invoked to cancel listening
- Throws:
IOException- If something goes wrong
-
findExecutable
Find an executable file with the passed file name, search the system path and the default search path (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin). Null is never returned - if nothing is found, returns a raw path name of the command name by itself.- Parameters:
command- The name of the executable; if the name is a file path, only the file name portion is used- Returns:
- A path, which may or may not exist.
-
findExecutable
Find an executable file with the passed file name, search the system path and the default search path (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin). Null is never returned - if nothing is found, returns a raw path name of the command name by itself.- Parameters:
command- The name of the executable; if the name is a file path, only the file name portion is usedalsoSearch- A path or list of paths to search first, preferring any executable of the right name found in these locations- Returns:
- A path, which may or may not exist.
-
findExecutable
public static Path findExecutable(String command, boolean useDefaultSearchPath, boolean useSystemPath, String... alsoSearch) Find an executable file with the passed file name, search the system path and the default search path (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin). Null is never returned - if nothing is found, returns a raw path name of the command name by itself.- Parameters:
command- The name of the executable; if the name is a file path, only the file name portion is useduseDefaultSearchPath- if true, search the default search path in addition to anyPATHvariable in the environment (ifuseSystemPathis true) (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin).useSystemPath- Search any system path provided by thePATHenvironment variablealsoSearch- A path or list of paths to search first, preferring any executable of the right name found in these locations- Returns:
- A path, which may or may not exist.
-
filesOnly
Get a predicate for filtering file streams which only lets files through.- Returns:
- A predicate
-
foldersOnly
Get a predicate for filtering file streams which only lets folders through.- Returns:
- A predicate
-
byExtension
Get a predicate for filtering file streams which only lets non-folders with a give file extension delimited by.through.- Returns:
- A predicate
-
find
Find all files in the subtree under a folder which have the given file extension.- Parameters:
dir- A folderextension- A file extension- Returns:
- A set of paths
- Throws:
IOException- If something goes wrong
-
find
Find all files in the subtree under a folder which have the given file extension.- Parameters:
dir- A folderrelativize- If true, returned paths will be relative to the passed folderextension- A file extension- Returns:
- A set of paths
- Throws:
IOException- If something goes wrong
-
search
public static int search(boolean relativize, Path dir, boolean followLinks, Predicate<? super Path> predicate, Consumer<? super Path> consumer) throws IOException Search the subtree of a directory for paths.- Parameters:
relativize- If true, pass the portion of the path relative to the folder, not the absolute path of each match.dir- The folderfollowLinks- If true, follow symlinkspredicate- A predicate to determine which are paths are passed to the consumerconsumer- A consumer to receive matches- Returns:
- The number of matches which were passed to the consumer
- Throws:
IOException- If something goes wrong
-
lines
Get a stream which lazily loads each line of a file as UTF-8 (unlike Files.readAllLines()) allowing very large files to be parsed line by line without iteration necessarily requiring more than(DEFAULT_BUFFER_SIZE * 2) + (maxLineLength * charset-max-bytes-per-char)bytes of memory.- Parameters:
path- the file path- Returns:
- A stream of lines. Note, since I/O is involved, the returned stream can throw undeclared IOExceptions
-
lines
Get a stream which lazily loads each line of a file (unlike Files.readAllLines()) allowing very large files to be parsed line by line without iteration necessarily requiring more than(bufferSize * 2) + (maxLineLength * charset-max-bytes-per-char)bytes of memory.- Parameters:
path- the file pathcharset- the character set- Returns:
- A stream of lines. Note, since I/O is involved, the returned stream can throw undeclared IOExceptions
-
lines
Get a stream which lazily loads each line of a file (unlike Files.readAllLines()) allowing very large files to be parsed line by line without iteration necessarily requiring more than(bufferSize * 2) + (maxLineLength * charset-max-bytes-per-char)bytes of memory.- Parameters:
path- the file pathbufferSize- the buffer size for the byte gathering buffer (the character buffer size will be one of two byte chars sized using a heuristic based on the charset's deocder's reported average bytes per character).charset- the character set- Returns:
- A stream of lines. Note, since I/O is involved, the returned stream can throw undeclared IOExceptions
-
filesToPaths
For adaptering old and new file apis, convert an iterator of files to an iterator of paths.- Parameters:
files- Some files- Returns:
- An iterator
-
pathsToFiles
For adaptering old and new file apis, convert an iterator of paths to an iterator of files.- Parameters:
paths- Some files- Returns:
- An iterator
-
filesToPaths
For adaptering old and new file apis, convert an iterator of files to an iterable of paths.- Parameters:
files- Some files- Returns:
- An iterator
-
pathsToFiles
For adaptering old and new file apis, convert an iterator of paths to an iterable of files.- Parameters:
paths- Some files- Returns:
- An iterator
-
ifExists
-
ifDirectory
-
ifExistsT
-
ifDirectoryT
-
copyFolderTree
Copy an entire folder tree.- Parameters:
log- A logfrom- The source folderto- The target folder- Returns:
- a 2-element array with the number of files copied and the number of folders created
- Throws:
IOException- if something goes wrong
-
userCacheRoot
Get the cache directory in the user's home directory - on linux, ~/.cache, on Mac OS X ~/Library/Caches- Returns:
- A Path
-
temp
Get the system temporary directory.- Returns:
- A path
-
home
-
homeRelativePath
Returns a shorter string prefixed with ~/ if the passed path is below the user's home dir.- Parameters:
what- A path- Returns:
- A string representation of the path that shortens it if it can.
-
unzip
Unzip the passed input stream into the passed directory.- Parameters:
in- An input streamdir- A folder- Throws:
IOException- if something goes wrong
-