Class FileUtils

java.lang.Object
com.mastfrog.util.file.FileUtils

public final class FileUtils extends Object
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
    Modifier and Type
    Field
    Description
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Predicate<Path>
    Get a predicate for filtering file streams which only lets non-folders with a give file extension delimited by . through.
    static int[]
    Copy an entire folder tree.
    static int
    decode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, boolean permissive)
     
    static int
    decode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, boolean permissive, CoderResult[] res)
     
    static int
    decode(ReadableByteChannel fileChannel, ByteBuffer readBuffer, CharBuffer target, CharsetDecoder charsetDecoder, Obj<CoderResult> res, boolean permissive)
     
    static boolean
    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 boolean
    deleteIfExists(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 void
    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.
    static Predicate<Path>
    Get a predicate for filtering file streams which only lets files through.
    static Iterable<Path>
    For adaptering old and new file apis, convert an iterator of files to an iterable of paths.
    static Iterator<Path>
    For adaptering old and new file apis, convert an iterator of files to an iterator of paths.
    static Set<Path>
    find(Path dir, boolean relativize, String extension)
    Find all files in the subtree under a folder which have the given file extension.
    static Set<Path>
    find(Path dir, String extension)
    Find all files in the subtree under a folder which have the given file extension.
    static Path
    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 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).
    static Path
    findExecutable(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).
    static Predicate<Path>
    Get a predicate for filtering file streams which only lets folders through.
    static Path
     
    static String
    Returns a shorter string prefixed with ~/ if the passed path is below the user's home dir.
    static Optional<Path>
     
     
    static Optional<Path>
    ifExists(Path path)
     
     
    lines(Path path)
    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.
    lines(Path path, int bufferSize, Charset charset)
    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.
    lines(Path path, Charset charset)
    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.
    newFile(Path dir, String name, String ext)
    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.
    newPath(Path dir, String name, String ext)
    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 Path
    Create a new temporary subfolder in the system temporary files folder.
    static Path
    Create a new temporary subfolder in the system temporary files folder.
    static Path
    newTempDir(String prefix, PosixFilePermission... permissions)
    Create a new temporary subfolder in the system temporary files folder.
    static Path
    Create a new temporary file on disk in the system temporary files folder, with default permissons.
    static Path
    Create a new temporary file in the system temporary files folder.
    static Path
    newTempFile(String prefix, PosixFilePermission... permissions)
    Create a new temporary file in the system temporary files folder.
    static Path
    Create a new temporary file on disk in the system temporary files folder.
    static Path
    Create a new file or directory path in the system temporary directory without creating anything on disk.
    static Iterable<File>
    For adaptering old and new file apis, convert an iterator of paths to an iterable of files.
    static Iterator<File>
    For adaptering old and new file apis, convert an iterator of paths to an iterator of files.
    Read an ASCII character sequence from a file using default settings and permissive handling of coding errors.
    static String
    Read an ASCII character sequence from a file using default settings and permissive handling of coding errors.
    readCharSequence(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.
    readCharSequence(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.
    readCharSequence(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.
    readCharSequence(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.
    readCharSequence(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 String
    readString(Path path, int bufferSize, Charset charset)
    Read a String from a file using default settings and permissive handling of coding errors.
    static String
    readString(Path path, int bufferSize, Charset charset, boolean permissive)
    Read a String from a file using default settings and permissive handling of coding errors.
    static String
    readString(Path path, Charset as)
    Read a String from a file using default settings and permissive handling of coding errors.
    readUTF8(Path path)
    Read a UTF-8 character sequence from a file using default settings and permissive handling of coding errors.
    static String
    Read a UTF-8 character sequence from a file using default settings and permissive handling of coding errors.
    static int
    search(boolean relativize, Path dir, boolean followLinks, Predicate<? super Path> predicate, Consumer<? super Path> consumer)
    Search the subtree of a directory for paths.
    static void
    setPermissions(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.
    tail(Path file, Charset charset)
    Tail a file, such as a log file.
    tail(Path file, Charset charset, int bufferSize, Executor exe)
    Tail a file, such as a log file.
    static Path
    Get the system temporary directory.
    static void
    Unzip the passed input stream into the passed directory.
    static Path
    Get the cache directory in the user's home directory - on linux, ~/.cache, on Mac OS X ~/Library/Caches
    static void
    writeAscii(Path path, CharSequence content)
    Write a character sequence to a file as ASCII.
    static <T extends WritableByteChannel>
    T
    writeCharSequence(CharSequence content, boolean permissive, Charset as, int bufferSize, boolean directBuffers, IOSupplier<? extends T> channelSupplier)
    Write a character sequence to a channel.
    static void
    writeFile(Path path, boolean permissive, CharSequence content, boolean directBuffers, Charset as, int bufferSize, boolean append, FileAttribute... attrs)
    Write a character sequence to a file.
    static void
    writeFile(Path path, boolean permissive, CharSequence content, Charset as, int bufferSize, boolean append, FileAttribute... attrs)
    Write a character sequence to a file.
    static void
    writeFile(Path path, CharSequence content, Charset as)
    Write a character sequence to a file.
    static void
    writeFile(Path path, CharSequence content, Charset as, boolean append, FileAttribute... attrs)
    Write a character sequence to a file.
    static void
    writeFile(Path path, CharSequence content, Charset as, int bufferSize, boolean append)
    Write a character sequence to a file.
    static void
    writeFile(Path path, CharSequence content, Charset as, int bufferSize, boolean append, FileAttribute... attrs)
    Write a character sequence to a file.
    static void
    writeUtf8(Path path, CharSequence content)
    Write a character sequence to a file as UTF-8.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • newFile

      public Path newFile(Path dir, String name, String ext) throws IOException
      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 folder
      name - The name
      ext - The extension
      Returns:
      The created file
      Throws:
      IOException
    • newPath

      public Path newPath(Path dir, String name, String ext) throws IOException
      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 folder
      name - The name
      ext - The extension
      Returns:
      The created file
      Throws:
      IOException
    • newTempDir

      public static Path newTempDir() throws IOException
      Create a new temporary subfolder in the system temporary files folder.
      Returns:
      A new folder created on disk
      Throws:
      IOException
    • newTempDir

      public static Path newTempDir(String prefix) throws IOException
      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

      public static Path newTempDir(String prefix, PosixFilePermission... permissions) throws IOException
      Create a new temporary subfolder in the system temporary files folder.
      Parameters:
      prefix - String prefix to prepend to the file name for identification
      permissions - The file permissions
      Returns:
      A new folder created on disk
      Throws:
      IOException - If something goes wrong
    • newTempFile

      public static Path newTempFile(PosixFilePermission... perms) throws IOException
      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

      public static Path newTempFile() throws IOException
      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

      public static Path newTempFile(String prefix) throws IOException
      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 identification
      permissions - The file permissions
      Returns:
      A new folder created on disk
      Throws:
      IOException - If something goes wrong
    • setPermissions

      public static void setPermissions(Path path, PosixFilePermission... permissions) throws IOException
      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 path
      permissions - A set of permissions
      Throws:
      IOException
    • deleteIfExists

      public static boolean deleteIfExists(Path first, Path... more) throws IOException
      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 path
      more - More files to delete
      Returns:
      True if any file was deleted
      Throws:
      IOException - If something else goes wrong
    • deleteIfExists

      public static boolean deleteIfExists(Path path) throws IOException
      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

      public static Path newTempPath(String prefix)
      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 path
      content - The character sequence to write
      as - The character set to use
      bufferSize - The size of buffer to use, trading performance for memory use as desired
      append - If true, append to the file, rather than truncating it if it exists
      Throws:
      IOException - If something goes wrong
    • writeUtf8

      public static void writeUtf8(Path path, CharSequence content) throws IOException
      Write a character sequence to a file as UTF-8.
      Parameters:
      path - The file path
      content - The character sequence to write
      Throws:
      IOException - If something goes wrong
    • writeAscii

      public static void writeAscii(Path path, CharSequence content) throws IOException
      Write a character sequence to a file as ASCII.
      Parameters:
      path - The file path
      permissive - If true, do not throw exceptions if characters are unencodable
      content - The character sequence to write
      as - The character set to use
      Throws:
      IOException - If something goes wrong
    • writeFile

      public static void writeFile(Path path, CharSequence content, Charset as) 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 path
      permissive - If true, do not throw exceptions if characters are unencodable
      content - The character sequence to write
      as - 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 path
      permissive - If true, do not throw exceptions if characters are unencodable
      content - The character sequence to write
      as - The character set to use
      append - If true, append to the file, rather than truncating it if it exists
      attrs - 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 path
      permissive - If true, do not throw exceptions if characters are unencodable
      content - The character sequence to write
      as - The character set to use
      bufferSize - The size of buffer to use, trading performance for memory use as desired
      append - If true, append to the file, rather than truncating it if it exists
      attrs - 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 path
      permissive - If true, do not throw exceptions if characters are unencodable
      content - The character sequence to write
      as - The character set to use
      bufferSize - The size of buffer to use, trading performance for memory use as desired
      append - If true, append to the file, rather than truncating it if it exists
      attrs - 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 path
      permissive - If true, do not throw exceptions if characters are unencodable
      content - The character sequence to write
      directBuffers - Use direct buffers - slower performance for small files, but one less memory copy
      as - The character set to use
      bufferSize - The size of buffer to use, trading performance for memory use as desired
      append - If true, append to the file, rather than truncating it if it exists
      attrs - 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 write
      permissive - If true, do not throw exceptions if characters are unencodable
      as - The character set to use
      bufferSize - The size of buffer to use, trading performance for memory use as desired
      directBuffers - Use direct buffers - slower performance for small files, but one less memory copy
      channelSupplier - The supplier which will open a writable channel
      Returns:
      The channel which was written to
      Throws:
      IOException - If something goes wrong
    • readAscii

      public static CharSequence readAscii(Path path) throws IOException
      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

      public static String readAsciiString(Path path) throws IOException
      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

      public static CharSequence readUTF8(Path path) throws IOException
      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

      public static String readUTF8String(Path path) throws IOException
      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

      public static String readString(Path path, Charset as) throws IOException
      Read a String from a file using default settings and permissive handling of coding errors.
      Parameters:
      path - The file
      as - The character set
      Returns:
      A character sequence
      Throws:
      IOException - If something goes wrong
    • readCharSequence

      public static CharSequence readCharSequence(Path path, 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 file
      as - The character set
      Returns:
      A character sequence
      Throws:
      IOException - If something goes wrong
    • readString

      public static String readString(Path path, int bufferSize, Charset charset) throws IOException
      Read a String from a file using default settings and permissive handling of coding errors.
      Parameters:
      path - The file
      bufferSize - The size of the buffer used to read, trading memory for performance or performance for memory footprint
      as - 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 file
      bufferSize - The size of the buffer used to read, trading memory for performance or performance for memory footprint
      as - 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 file
      bufferSize - The size of the buffer used to read, trading memory for performance or performance for memory footprint
      as - The character set
      permissive - 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 file
      bufferSize - The size of the buffer used to read, trading memory for performance or performance for memory footprint
      as - The character set
      permissive - 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 file
      directBuffers - If true, use NIO direct buffers for reads, trading some performance for one less memory copy
      bufferSize - The size of the buffer used to read, trading memory for performance or performance for memory footprint
      as - The character set
      permissive - 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 copy
      bufferSize - The number of bytes to read at a time to manage memory pressure
      charset - The character set
      permissive - If true, ignore encoding errors
      channelHolder - 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 supplier
      ch - 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

      public static void deltree(Path dir) throws IOException
      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 lines
      bufferSize - The size of the internal buffer used to read bytes from disk
      exe - 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

      public static Path findExecutable(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). 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

      public static Path findExecutable(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). 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
      alsoSearch - 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 used
      useDefaultSearchPath - if true, search the default search path in addition to any PATH variable in the environment (if useSystemPath is true) (/usr/bin:/usr/local/bin:/opt/local/bin:/bin:/sbin:/usr/sbin:/opt/bin).
      useSystemPath - Search any system path provided by the PATH environment variable
      alsoSearch - 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

      public static Predicate<Path> filesOnly()
      Get a predicate for filtering file streams which only lets files through.
      Returns:
      A predicate
    • foldersOnly

      public static Predicate<Path> foldersOnly()
      Get a predicate for filtering file streams which only lets folders through.
      Returns:
      A predicate
    • byExtension

      public static Predicate<Path> byExtension(String ext)
      Get a predicate for filtering file streams which only lets non-folders with a give file extension delimited by . through.
      Returns:
      A predicate
    • find

      public static Set<Path> find(Path dir, String extension) throws IOException
      Find all files in the subtree under a folder which have the given file extension.
      Parameters:
      dir - A folder
      extension - A file extension
      Returns:
      A set of paths
      Throws:
      IOException - If something goes wrong
    • find

      public static Set<Path> find(Path dir, boolean relativize, String extension) throws IOException
      Find all files in the subtree under a folder which have the given file extension.
      Parameters:
      dir - A folder
      relativize - If true, returned paths will be relative to the passed folder
      extension - 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 folder
      followLinks - If true, follow symlinks
      predicate - A predicate to determine which are paths are passed to the consumer
      consumer - A consumer to receive matches
      Returns:
      The number of matches which were passed to the consumer
      Throws:
      IOException - If something goes wrong
    • lines

      public static Stream<CharSequence> lines(Path path)
      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

      public static Stream<CharSequence> lines(Path path, Charset charset)
      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 path
      charset - the character set
      Returns:
      A stream of lines. Note, since I/O is involved, the returned stream can throw undeclared IOExceptions
    • lines

      public static Stream<CharSequence> lines(Path path, int bufferSize, Charset charset)
      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 path
      bufferSize - 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

      public static Iterator<Path> filesToPaths(Iterator<File> files)
      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

      public static Iterator<File> pathsToFiles(Iterator<Path> paths)
      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

      public static Iterable<Path> filesToPaths(Iterable<File> file)
      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

      public static Iterable<File> pathsToFiles(Iterable<Path> file)
      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

      public static Optional<Path> ifExists(Path path)
    • ifDirectory

      public static Optional<Path> ifDirectory(Path path)
    • ifExistsT

      public static ThrowingOptional<Path> ifExistsT(Path path)
    • ifDirectoryT

      public static ThrowingOptional<Path> ifDirectoryT(Path path)
    • copyFolderTree

      public static int[] copyFolderTree(Path from, Path to) throws IOException
      Copy an entire folder tree.
      Parameters:
      log - A log
      from - The source folder
      to - 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

      public static Path userCacheRoot()
      Get the cache directory in the user's home directory - on linux, ~/.cache, on Mac OS X ~/Library/Caches
      Returns:
      A Path
    • temp

      public static Path temp()
      Get the system temporary directory.
      Returns:
      A path
    • home

      public static Path home()
    • homeRelativePath

      public static String homeRelativePath(Path what)
      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

      public static void unzip(InputStream in, Path dir) throws IOException
      Unzip the passed input stream into the passed directory.
      Parameters:
      in - An input stream
      dir - A folder
      Throws:
      IOException - if something goes wrong