Class IO

java.lang.Object
com.pnfsoftware.jeb.util.io.IO

public class IO extends Object
File manipulation utility routines.
  • Field Details

    • MAX_FILENAME_SIZE

      public static final int MAX_FILENAME_SIZE
      Maximum filename size used in JEB (255 allowed for Linux, 259 for Windows file)
      See Also:
    • MAX_PATH_SIZE

      public static final int MAX_PATH_SIZE
      Maximum foldername size used in JEB (248 for Windows folder)
      See Also:
  • Constructor Details

    • IO

      public IO()
  • Method Details

    • setCwd

      public static String setCwd(String cwd)
      Set the path of the current working directory.

      Note: handle with care, changing the CWD may not impact all subsequent file read and write operations. It is safer to retrieve and use the CWD with getCwd().

      Parameters:
      cwd - new current working directory
      Returns:
      previous working directory
    • getCwd

      public static String getCwd()
      Get the path of the current working directory.
      Returns:
      current working directory path
    • getOriginalCwd

      public static String getOriginalCwd()
      Get original working directory, i.e. the value of the current directory set by the JVM when the program was started.

      This method is unreliable. It assumes that all updates to the current directory are done through this class.

      Returns:
      original working directory path
    • isFile

      public static boolean isFile(String path)
      Determine if a path refers to an existing file.
      Parameters:
      path - path to test
      Returns:
      true if the path exists and is a regular file
    • createFolder

      public static File createFolder(String path) throws IOException
      Create or retrieve a directory.
      Parameters:
      path - directory path
      Returns:
      the directory file object
      Throws:
      IOException - on creation error
    • createDirectory

      public static boolean createDirectory(String path)
      Safely create a directory.
      Parameters:
      path - directory path
      Returns:
      true on success, else false
    • createDirectory

      public static boolean createDirectory(File f)
      Safely create a directory.
      Parameters:
      f - directory to create
      Returns:
      true on success, else false
    • deleteDirectory

      public static boolean deleteDirectory(String path)
      Delete a directory and its contents recursively.
      Parameters:
      path - directory path
      Returns:
      true on success
    • deleteDirectory

      public static boolean deleteDirectory(File dir)
      Delete a directory and its contents recursively.
      Parameters:
      dir - directory to delete
      Returns:
      true on success, false if errors were encountered; note that some files and directories may have been deleted
    • deleteDirectoryContents

      public static boolean deleteDirectoryContents(File dir)
      Delete a directory contents recursively (ie, clear the directory). The directory itself is not deleted.
      Parameters:
      dir - directory whose contents should be deleted
      Returns:
      true on success, false if errors were encountered; note that some files and directories may have been deleted
    • deleteFile

      public static boolean deleteFile(File file)
      Delete a file.
      Parameters:
      file - the file to be deleted
      Returns:
      true on success
    • renameFile

      public static boolean renameFile(File src, File dst, int mode)
      Rename a file with options.
      Parameters:
      src - source file
      dst - destination
      mode - define the mechanics of the renaming operations:
      • 0: standard rename, fail if 'dst' already exists or 'src' does not exist
      • 1: overwrite 'dst' if 'dst' already exists
      • 2: delete 'src' if 'dst' already exists
      Returns:
      true on success
    • listFiles

      public static List<File> listFiles(String folderpath)
      Recursively do a directory listing. The list returned is a list of true files (not directories). Refer to the DirectoryEnumerator object for a more powerful file enumerator.
      Parameters:
      folderpath - folder path
      Returns:
      the list of absolute file paths
    • listFiles

      public static List<File> listFiles(File folder)
      Recursively do a directory listing. The list returned is a list of true files (not directories). Refer to the DirectoryEnumerator object for a more powerful file enumerator.
      Parameters:
      folder - folder to list
      Returns:
      the list of absolute files
    • safeList

      public static String[] safeList(File f)
      A safe version of File.list().
      Parameters:
      f - a non-null file object
      Returns:
      non null array
    • safeList

      public static String[] safeList(File f, FilenameFilter filter)
      A safe version of File.list(FilenameFilter).
      Parameters:
      f - a non-null file object
      filter - a file filter
      Returns:
      non-null array
    • safeListFiles

      public static File[] safeListFiles(File f)
      A safe version of File.listFiles().
      Parameters:
      f - a non-null file object
      Returns:
      non null array
    • safeListFiles

      public static File[] safeListFiles(File f, FileFilter filter)
      A safe version of File.listFiles(FileFilter).
      Parameters:
      f - a non-null file object
      filter - a file filter
      Returns:
      non-null array
    • inFolder

      public static boolean inFolder(File file, File folder)
      Determine if a hypothetical file would be contained within a hypothetical folder, i.e. whether or not the folder would be an ancestor of the file.
      Parameters:
      file - file or folder to test (whose existence is not required)
      folder - candidate ancestor folder (whose existence is not required)
      Returns:
      true if the candidate folder looks like an ancestor folder of the file
    • getHomeFolder

      public static File getHomeFolder()
      Retrieve the current user's home folder.
      Returns:
      current user's home folder
    • getTempFolder

      public static File getTempFolder()
      Retrieve the temporary folder.
      Returns:
      the temp folder, never null
    • getSessionTemporaryFolder

      public static File getSessionTemporaryFolder()
      Retrieve the JEB session-specific temporary folder. That folder is unique per session, and can be used to drop additional temporary files and folders. Its contents is deleted when the session ends (i.e. when the owning JEB instance is terminated).
      Returns:
      this JEB session's temporary folder
    • createTempFolder

      public static File createTempFolder(String folderName) throws IOException
      Create a directory in the default temporary-file directory.
      Parameters:
      folderName - optional folder name; if null, a randomly named folder will be created
      Returns:
      a File representing the newly created directory, or null on error
      Throws:
      IOException - if an IO exception happened
    • createTempFile

      public static File createTempFile() throws IOException
      Create an empty file in the temporary folder.
      Returns:
      the file
      Throws:
      IOException - on file creation error
    • createSafeTempFile

      public static File createSafeTempFile() throws IOException
      Create an empty file in the safe temporary area.
      Returns:
      the file
      Throws:
      IOException - on file creation error
    • createTempFile

      public static File createTempFile(String basename, String extension) throws IOException
      Create a file in the default temp folder.
      Parameters:
      basename - optional base name
      extension - optional extension (suffix)
      Returns:
      created temporary file
      Throws:
      IOException - on file creation error
    • createTempFile

      public static File createTempFile(String exactname)
      Create a file (with an exact name) in the default temp folder.
      Parameters:
      exactname - exact file name
      Returns:
      the file object (the file itself may or may not exist when the method returns)
    • createTempFileNumbered

      public static File createTempFileNumbered(String name) throws IOException
      Create a new file with the provided name in the default temp folder. If a file with the provided name already exists, counter-suffixed names are tried (NAME.1, NAME.2, etc.) until the first non-existent entry is found.
      Parameters:
      name - desired file name
      Returns:
      the file object (the file itself does not exist at the time this method returns)
      Throws:
      IOException - if a free numbered name cannot be found
    • createFile

      public static boolean createFile(File file, boolean createDirs) throws IOException
      Create an empty file.
      Parameters:
      file - file to create
      createDirs - if true, the directory structure needed to contain the file will also be created
      Returns:
      true if the named file does not exist and was successfully created; false if the named file already exists
      Throws:
      IOException - if the file cannot be created
    • createFoldersForFile

      public static void createFoldersForFile(File file) throws IOException
      Create the directory structure needed to store a file.
      Parameters:
      file - a file
      Throws:
      IOException - if a parent directory cannot be created
    • deleteDirectoryOnExit

      public static void deleteDirectoryOnExit(File dir)
      Delete a folder and all its contents recursively, when the virtual machine terminates.
      Parameters:
      dir - folder to be deleted
    • copyFile

      public static void copyFile(File src, File dst, boolean overwrite) throws IOException
      Copy a source file to a destination. The copy process is interruptible.
      Parameters:
      src - source file (not a directory)
      dst - destination file or directory
      overwrite - if true, the destination file may be overwritten if it already exists
      Throws:
      FileNotFoundException - the source is not found
      FileAlreadyExistsException - the destination would be overwritten
      IOException - other IO exception
    • copyStream

      public static long copyStream(InputStream input, OutputStream output, byte[] buffer) throws IOException
      Read bytes from an input stream and dump them to an output stream. The copy process is interruptible.
      Parameters:
      input - source stream
      output - destination stream
      buffer - recommended use: multiple of 0x1000
      Returns:
      number of bytes copied
      Throws:
      IOException - if copying fails or is interrupted
    • copyStream

      public static long copyStream(InputStream input, OutputStream output) throws IOException
      Read bytes from an input stream and dump them to an output stream. The copy process is interruptible.
      Parameters:
      input - source stream
      output - destination stream
      Returns:
      number of bytes copied
      Throws:
      IOException - if copying fails or is interrupted
    • copy

      @Deprecated public static long copy(InputStream input, OutputStream output) throws IOException
      Throws:
      IOException
    • writeFile

      public static void writeFile(File file, byte[] data, int offset, int size, boolean createDirs) throws IOException
      Write data to a file whose containing directory structure may not exist. Existing file contents will be overwritten.
      Parameters:
      file - output file
      data - source bytes
      offset - first source byte to write
      size - number of bytes to write
      createDirs - create the intermediate directories if need be
      Throws:
      IOException - if the file cannot be written
    • writeFile

      public static void writeFile(File file, byte[] data, boolean createDirs) throws IOException
      Write data to a file whose containing directory structure may not exist. Existing file contents will be overwritten.
      Parameters:
      file - output file
      data - source bytes
      createDirs - create the intermediate directories if need be
      Throws:
      IOException - if the file cannot be written
    • writeFile

      public static void writeFile(File file, byte[] data, int offset, int size) throws IOException
      Write data to a file. Existing file contents will be overwritten.
      Parameters:
      file - output file
      data - source bytes
      offset - first source byte to write
      size - number of bytes to write
      Throws:
      IOException - if the file cannot be written
    • writeFile

      public static void writeFile(File file, byte[] data) throws IOException
      Write data to a file. Existing file contents will be overwritten.
      Parameters:
      file - destination file
      data - binary data to write
      Throws:
      IOException - on error
    • writeFile

      @Deprecated public static void writeFile(File file, String str) throws IOException
      Throws:
      IOException
    • writeFile

      @Deprecated public static void writeFile(File file, String str, String charsetName) throws IOException
      Deprecated.
      use
      invalid reference
      #writeTextFile(File, String, String)
      Throws:
      IOException
    • writeTextFile

      public static void writeTextFile(File file, String str) throws IOException
      Write a string to a file using the default character encoding of the current system. Existing file contents will be overwritten.
      Parameters:
      file - destination file
      str - string
      Throws:
      IOException - on error
    • writeTextFile

      public static void writeTextFile(File file, String str, Charset encoding) throws IOException
      Write a string to a file using the given character encoding of the current system. Existing file contents will be overwritten.
      Parameters:
      file - output file
      str - string to write
      charsetName - character set name
      Throws:
      IOException - if the file cannot be written
    • readFile

      public static byte[] readFile(File file, long maxAllowedSize) throws IOException
      Read the contents of a binary file that is at most 2 Gb.
      Parameters:
      file - file to read
      maxAllowedSize - optional maximum size, -1 means the max (2 Gb)
      Returns:
      the contents of the file
      Throws:
      IOException - on IO error, or if the read size exceeds the limit
    • readFile

      public static byte[] readFile(File file) throws IOException
      Read the contents of a binary file that is at most 2 Gb.
      Parameters:
      file - file to read
      Returns:
      the contents of the file
      Throws:
      IOException - on IO error, or if the read size exceeds 2 Gb
    • readFile

      public static byte[] readFile(String path) throws IOException
      Read the contents of a binary file.
      Parameters:
      path - input file path
      Returns:
      file contents
      Throws:
      IOException - on IO error, or if the read size exceeds 2 Gb
    • readInputStream

      public static byte[] readInputStream(InputStream in) throws IOException
      Read and reliably return all bytes of an input stream. The stream must be less than 2Gb. The caller is responsible for closing the input stream after reading.
      Parameters:
      in - input stream
      Returns:
      stream contents
      Throws:
      IOException - on IO error, or if the read size exceeds 2 Gb
    • processInputStream

      public static void processInputStream(InputStream in, BiConsumer<byte[],Integer> consumer) throws IOException
      Process an input stream. The method returns after all input bytes are read.
      Parameters:
      in - input stream (it will be read via a BufferedInputStream)
      consumer - the consumer of read data: first input= bytes array; second input: the count of bytes read
      Throws:
      IOException - on IO error
    • readTextFile

      public static String readTextFile(File file) throws IOException
      Read a file and decode it to a string using the system's default encoding.
      Parameters:
      file - input file
      Returns:
      decoded text
      Throws:
      IOException - on IO error
    • readTextFile

      public static String readTextFile(File file, Charset encoding) throws IOException
      Read a file and decode it to a string using the system's default locale.
      Parameters:
      file - input file
      encoding - requested encoding (if null, the system's default encoding is used)
      Returns:
      decoded text
      Throws:
      IOException - on IO error
    • readLines

      public static List<String> readLines(InputStream input, Charset encoding) throws IOException
      Read the contents of a stream as a line of strings.
      Parameters:
      input - the input stream
      encoding - the stream encoding
      Returns:
      a list of strings, without new-line characters
      Throws:
      IOException - on IO error
    • readLines

      public static List<String> readLines(InputStream input) throws IOException
      Read the contents of a stream as a text buffer of UTF-8 encoded strings.
      Parameters:
      input - input stream
      Returns:
      a list of strings, without new-line characters
      Throws:
      IOException - on IO error
    • readLines

      public static List<String> readLines(File file, Charset encoding) throws IOException
      Read the lines of a text file.
      Parameters:
      file - input file
      encoding - file encoding
      Returns:
      a list of strings, without new-line characters
      Throws:
      IOException - on IO error
    • readLines

      public static List<String> readLines(File file) throws IOException
      Read the UTF-8 encoded lines of a text file.
      Parameters:
      file - input file
      Returns:
      a list of strings, without new-line characters
      Throws:
      IOException - on IO error
    • readLinesSafe

      public static List<String> readLinesSafe(File file, Charset encoding)
      Read the lines of a text file. On error, the method returns null.
      Parameters:
      file - input file
      encoding - file encoding
      Returns:
      a list of lines without newline chars, null on error
    • readLinesSafe

      public static List<String> readLinesSafe(File file)
      Read the lines of a UTF8 encoded text file. On error, the method returns null.
      Parameters:
      file - input file
      Returns:
      a list of lines without newline chars, null on error
    • writeLines

      public static void writeLines(OutputStream output, List<? extends CharSequence> lines, Charset encoding) throws IOException
      Write strings.
      Parameters:
      output - output stream
      lines - lines to write
      encoding - output encoding
      Throws:
      IOException - on IO error
    • writeLines

      public static void writeLines(OutputStream output, List<? extends CharSequence> lines) throws IOException
      Write UTF-8 encoded strings.
      Parameters:
      output - output stream
      lines - lines to write
      Throws:
      IOException - on IO error
    • writeLines

      public static void writeLines(File file, List<? extends CharSequence> lines, Charset encoding) throws IOException
      Write strings.
      Parameters:
      file - output file
      lines - lines to write
      encoding - output encoding
      Throws:
      IOException - on IO error
    • writeLines

      public static void writeLines(File file, List<? extends CharSequence> lines) throws IOException
      Write UTF-8 encoded strings.
      Parameters:
      file - output file
      lines - lines to write
      Throws:
      IOException - on IO error
    • writeLinesSafe

      public static boolean writeLinesSafe(OutputStream output, List<? extends CharSequence> lines, Charset encoding)
      Write strings. On error, the method returns false.
      Parameters:
      output - output stream
      lines - lines to write
      encoding - output encoding
      Returns:
      true on success
    • writeLinesSafe

      public static boolean writeLinesSafe(OutputStream output, List<? extends CharSequence> lines)
      Write UTF-8 encoded strings. On error, the method returns false.
      Parameters:
      output - output stream
      lines - lines to write
      Returns:
      true on success
    • writeLinesSafe

      public static boolean writeLinesSafe(File file, List<? extends CharSequence> lines, Charset encoding)
      Write strings. On error, the method returns false.
      Parameters:
      file - output file
      lines - lines to write
      encoding - output encoding
      Returns:
      true on success
    • writeLinesSafe

      public static boolean writeLinesSafe(File file, List<? extends CharSequence> lines)
      Write UTF-8 encoded strings. On error, the method returns false.
      Parameters:
      file - output file
      lines - lines to write
      Returns:
      true on success
    • getFirstLongLE

      public static long getFirstLongLE(File file)
      Get the first 8-byte of a file, read as a little-endian long. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
      Parameters:
      file - input file
      Returns:
      the first long, 0 on error
    • getFirstIntLE

      public static int getFirstIntLE(String path)
      Get the first 4-byte of a file, read as a little-endian integer. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
      Parameters:
      path - file path
      Returns:
      the first int, 0 on error
    • getFirstShortLE

      public static short getFirstShortLE(String path)
      Get the first 2-byte of a file, read as a little-endian short. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
      Parameters:
      path - input file path
      Returns:
      first two bytes interpreted as a little-endian short, or 0 on error
    • getFirstByte

      public static byte getFirstByte(String path)
      Get the first byte of a file. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
      Parameters:
      path - input file path
      Returns:
      first byte, or 0 on error
    • compressFolder

      public static boolean compressFolder(String folderPath, String zipfilePath)
      Compress a folder and its contents to a zip archive.
      Parameters:
      folderPath - input folder
      zipfilePath - output zip file
      Returns:
      true on success
    • compressFolder

      public static boolean compressFolder(File folder, File zipfile)
      Compress a folder and its contents to a zip archive.
      Parameters:
      folder - input folder
      zipfile - output zip file
      Returns:
      true on success
    • extractToFolder

      public static void extractToFolder(File inputZipFile, File outputFolder) throws IOException
      Extract the contents of a zip archive to a target folder.
      Parameters:
      inputZipFile - input zip file path
      outputFolder - output folder directory
      Throws:
      IOException - if an error occurred; the current state of extraction within the target folder is not erased
    • addFileToZip

      public static void addFileToZip(ZipOutputStream zip, File file, String entryName) throws IOException
      Add a new entry to a zip output stream.
      Parameters:
      zip - destination zip stream
      file - source file
      entryName - archive entry name
      Throws:
      IOException - on IO error
    • addFileToJar

      public static void addFileToJar(JarOutputStream jar, File file, String entryName) throws IOException
      Add a new entry to a jar output stream.
      Parameters:
      jar - destination jar stream
      file - source file
      entryName - archive entry name
      Throws:
      IOException - on IO error
    • readFileSafe

      public static byte[] readFileSafe(File file)
      Read a binary file safely. If any error happens, an empty array is returned.
      Parameters:
      file - input file
      Returns:
      the bytes in the file, never null (empty array on error)
    • writeFileSafe

      public static boolean writeFileSafe(File file, byte[] data, int offset, int size, boolean createDirs)
      Write a binary file safely.
      Parameters:
      file - output file
      data - source bytes
      offset - first source byte to write
      size - number of bytes to write
      createDirs - create the intermediate directories if need be
      Returns:
      true on success
    • writeFileSafe

      public static boolean writeFileSafe(File file, byte[] data, boolean createDirs)
      Write a binary file safely.
      Parameters:
      file - output file
      data - source bytes
      createDirs - create the intermediate directories if need be
      Returns:
      true on success
    • readInputLineSafe

      public static String readInputLineSafe()
      Read a line from the standard input. This method is safe to use; it does not raise, instead returning a null string on error.
      Returns:
      input line, or null on error
    • readInputLine

      public static String readInputLine() throws IOException
      Read a line from the standard input.
      Returns:
      input line
      Throws:
      IOException - on IO error
    • getParentFile2

      public static File getParentFile2(File file)
      A safer implementation of File.getParentFile(). If the input file is a single-level relative path (eg, "abc"), this method will first retrieve the absolute path (relative to the current folder) before trying to retrieve the parent folder.
      Parameters:
      file - file or folder
      Returns:
      null if there is no parent folder, eg in the case of a root folder
    • expandPath

      public static String expandPath(String path)
      Perform expansion of certain shell artifacts used in path names.

      Currently, the expansions performed are:

      • a leading tilde is expanded to a user's home directory
      Parameters:
      path - input path
      Returns:
      a full path name, if possible - otherwise, the original path is returned
    • copyAsync

      public static Thread copyAsync(InputStream input, OutputStream output)
      Copy a continuous stream of data from a source to a destination. This method creates a thread; it is non-blocking and returns immediately.
      Parameters:
      input - the source input stream
      output - the destination output stream
      Returns:
      the daemon worker thread in charge of copying the data
    • dirname

      public static String dirname(String path)
      Return the directory name of the provided path. The returned string may be empty. It will is not terminated by slash or backslash.

      Implementation note: mirrors Python 3's os.path.dirname.

      Parameters:
      path - a path
      Returns:
      never null
    • basename

      public static String basename(String path)
      Return the base name of the provided path. The base name is the part of the path that follows the last file separator character (slash or backslash). No further processing is done. The returned string may be empty or blank. It does not contain slash or backslash.

      Implementation note: mirrors Python 3's os.path.basename.

      Parameters:
      path - a path
      Returns:
      never null
    • parsePathElements

      public static List<String> parsePathElements(String s)
      Parse the elements of a path.
      Parameters:
      s - a path
      Returns:
      a list of path elements, possibly empty
    • escapeFileName

      public static String escapeFileName(String filename)
      Escape filename invalid characters with an underscore.

      For proper path sanitization, use sanitizePath(String, boolean, boolean).

      Parameters:
      filename - to escape
      Returns:
      the escaped filename
    • escapeFileName

      public static String escapeFileName(String filename, char newChar)
      Escape illegal characters in filename by the provided char. Note than this is a black-list escaper (whitelist character would be too costly to manage any char) so file name can still be irrelevant on the target Operating System. The list of excluded characters are :, \, /, *, ", ?, |, <, and >, which covers most of the invalid characters.

      For proper path sanitization, use sanitizePath(String, boolean, boolean).

      Parameters:
      filename - to escape
      newChar - replacement character
      Returns:
      the escaped filename
    • escapeFileNameStrict

      public static String escapeFileNameStrict(String filename)
      Create a valid filename with an underscore for non alphanumeric char, '-', '_', '.' nor '''.

      For proper path sanitization, use sanitizePath(String, boolean, boolean).

      Parameters:
      filename - to escape
      Returns:
      the escaped filename
    • escapeFileNameStrict

      public static String escapeFileNameStrict(String filename, char newChar)
      Create a valid filename from any string. Compared to escapeFileName(String, char), this is a white-list escaper so generated filename will be legal on any non-exotic target Operating System. The list of allowed characters are any alphanumeric char, '-', '_', '.', '''.

      For proper path sanitization, use sanitizePath(String, boolean, boolean).

      Parameters:
      filename - to escape
      newChar - replacement character
      Returns:
      the escaped filename
    • sanitizePathUnsafe

      public static String sanitizePathUnsafe(String path)
      Attempt to sanitize a path.

      Under some conditions, the returned path may be unsafe, eg on Windows: the sanitization of a path like "x::z" will generate an unsafe path. You must use sanitizePath(String, boolean, boolean) to guarantee sanitization.

      Parameters:
      path - input path
      Returns:
      sanitized path
    • sanitizePath

      public static String sanitizePath(String path, boolean isSinglePathElement, boolean replaceBlankCharacters)
      Sanitize a path. The returned path is guaranteed to be legal on the current filesystem (FS) if and only if the element being sanitized is a single path element. For full paths elements, sanitization cannot be guaranteed because some characters which could yield to bad paths, cannot be safely sanitized.

      Notes:
      - the following characters are always sanitized: ? % * | < > "
      - if a single path element is provided, the following characters are sanitized as well: / \ :

      Parameters:
      path - a path (absolute or not)
      isSinglePathElement - true if the provided path should be treated as a single path element, allowing more aggressive replacements
      replaceBlankCharacters - if true, sanitize all blank characters (per the Unicode definition of a blank character) as well, even though some may be legal
      Returns:
      a sanitized path, never null, guaranteed to be valid on the current FS
    • getRelativePath

      public static String getRelativePath(File file, File base)
      Provide the path to a file relative to a base.
      Parameters:
      file - a file
      base - the base to check for
      Returns:
      null if `file` is not rooted in `base`; else, the relative path of `file` (relative to `base`)
    • compareFiles

      public static boolean compareFiles(File file0, File file1) throws IOException
      Compare the contents of two files for equality.
      Parameters:
      file0 - first file
      file1 - second file
      Throws:
      FileNotFoundException - one of the input file is not found
      IOException - other IO exception
    • splitPath

      public static String[] splitPath(String path)
      Split a path into its base part and its filename part.

      Unlike File.getName(), this method considers both slash and backslash to be valid path separators.

      Parameters:
      path - input path
      Returns:
      a two-element array: the first element contains the base and may be empty (if non-empty, it will always end by a separator); the second element is the name, which does not contain any separator
    • splitExtension

      public static String[] splitExtension(String path)
      Split a file path into its extension-less part and its extension.
      Parameters:
      path - a file path
      Returns:
      a 2-element string array; the extension, if any, is the second element and always start with '.' unless it's empty
    • getExtension

      public static String getExtension(String path)
      Extract the extension of a file path.
      Parameters:
      path - a path
      Returns:
      the extension (always starting with a dot), or the empty string
    • getExtension

      public static String getExtension(File file)
      Extract the extension of a file path.
      Parameters:
      file - a file
      Returns:
      the extension (always starting with a dot), or the empty string
    • noExtension

      public static String noExtension(String path)
      Remove the extension from a file path.
      Parameters:
      path - input path
      Returns:
      path without its extension
    • noExtension

      public static File noExtension(File file)
      Remove the extension from a file path.
      Parameters:
      file - input file
      Returns:
      file without its extension
    • replaceExtension

      public static File replaceExtension(File file, String extension)
      Parameters:
      file - input file
      extension - the new extension, eg ".xyz"
      Returns:
      file with the new extension
    • abs

      public static String abs(File file)
      Provide an absolute simplified path. This method is OS-dependent.

      Rules:
      - . and .. uses are resolved.
      - Multiple-path separator uses such as /// or \\ are resolved.
      - Redundant trailing separators are removed.
      - The empty path is always resolved to ..
      - If the FS is case-insensitive (e.g. NTFS), the case is not standardized (Windows).
      - Symbolic links are not resolved.

      Parameters:
      file - input file
      Returns:
      absolute simplified path
    • abs

      public static String abs(String path)
      Provide an absolute simplified path. This method is OS-dependent.

      Rules:
      - . and .. uses are resolved.
      - Multiple-path separator uses such as /// or \\ are resolved.
      - Redundant trailing separators are removed.
      - The empty path is always resolved to ..
      - If the FS is case-insensitive (e.g. NTFS), the case is not standardized (Windows).
      - Symbolic links are not resolved.

      Parameters:
      path - input path
      Returns:
      absolute simplified path
    • simplifyPath

      public static String simplifyPath(String path)
      Simplify a path. This method is OS-dependent. To call a specific (Unix or Windows) version of this method, use simplifyPathUnix(String) or simplifyPathWindows(String).

      Rules:
      - . and .. uses are resolved.
      - Multiple-path separator uses such as /// or \\ are resolved.
      - Redundant trailing separators are removed.
      - The empty path is always resolved to ..
      - If the FS is case-insensitive (e.g. NTFS), the case is not standardized (Windows).
      - Symbolic links are not resolved.

      Parameters:
      path - input path
      Returns:
      absolute simplified path
    • simplifyPathUnix

      public static String simplifyPathUnix(String path)
    • simplifyPathWindows

      public static String simplifyPathWindows(String path)
    • checkFileFreshness

      public static boolean checkFileFreshness(File file, long millis, boolean cleanStaleFile)
      Parameters:
      file - file to check
      millis - freshness threshold in milliseconds
      cleanStaleFile - true to delete stale files
      Returns:
      true if the file exists and its last modification timestamp is less than millis ms old