Package de.uplanet.io

Class NIOHelper


  • @Scriptable
    public final class NIOHelper
    extends Object
    NIOHelper class utility.
    Since:
    Intrexx 8.0.
    • Method Detail

      • getFileName

        public static String getFileName​(Path p_path)
        Get the name of the farthest element from the root of the given path as a string. The call is equivalent to
         p_path.getFileName().toString()
         
        This method is intended to be referenced in places where otherwise clumsy lambda expressions were needed.
        Parameters:
        p_path - The path.
        Returns:
        The file name as a string.
        Since:
        Intrexx 9.0.
      • getNormalizedPath

        public static String getNormalizedPath​(Path p_file)
        Get the normalized path of an existing file or directory.

        The returned path's file separators are replaced with forward slashes ('/') if the operating system's file separator is not the forward slash.

        Note: This method is not equivalent to IOHelper.getNormalizedPath(File) since it does not necessarily return an absolute path and does not append a trailing slash to paths that refer to a directory.

        Parameters:
        p_file - The file.
        Returns:
        The normalized path.
        Since:
        Intrexx 9.0.
      • getPathRelative

        public static String getPathRelative​(Path p_dir,
                                             Path p_file)
                                      throws IOException
        Get the path of an existing file or directory below the given existing directory relative to this directory.

        The returned path's file separators are normalized to forward slash ('/') if the operating system's file separator is not the forward slash.

        If p_file denotes a directory the returned path, if not null, has a trailing forward slash.

        Parameters:
        p_dir - The base directory.
        p_file - The file for which the path relative to p_dir should be determined.
        Returns:
        The path of p_file relative to p_dir, or null if p_dir is not a parent of p_file.
        Throws:
        IOException - - If an I/O error occurred.
        Since:
        Intrexx 9.0.
      • contains

        public static boolean contains​(Path p_dir,
                                       DirectoryStream.Filter<Path> p_filter)
                                throws NotDirectoryException,
                                       IOException
        Check if the given directory contains files or directories that are accepted by the given filter.
        Parameters:
        p_dir - The directory.
        p_filter - The filter,
        Returns:
        true if the directory contains files or directories that are accepted by the given filter, or false otherwise.
        Throws:
        NotDirectoryException - If the given path does not represent an existing directory.
        IOException - If an I/O error occurred.
        IllegalArgumentException - If the given path is null.
        Since:
        Intrexx 9.0.
      • getFileSize

        public static long getFileSize​(Path p_file)
        Returns the size of the file or directory tree.
        Parameters:
        p_file - file or directory
        Returns:
        size of all files of the specified tree or of a single file
        Since:
        Intrexx 8.0.
      • getMessageDigestHex

        public static String getMessageDigestHex​(Path p_file,
                                                 MessageDigest p_md)
                                          throws IOException
        Create a lower-case hexadecimal encoded message digest from the given file.
        Parameters:
        p_file - The file.
        p_md - The message digest to be used.
        Returns:
        The message digest.
        Throws:
        FileNotFoundException - If the file does not exist.
        IOException - If an I/O error occurred.
        Since:
        Intrexx 9.0.
      • getMessageDigest

        public static byte[] getMessageDigest​(Path p_file,
                                              MessageDigest p_md)
                                       throws IOException
        Create a message digest from the given file.
        Parameters:
        p_file - The file.
        p_md - The message digest to be used.
        Returns:
        The message digest.
        Throws:
        FileNotFoundException - If the file does not exist.
        IOException - If an I/O error occurred.
        Since:
        Intrexx 9.0.
      • getFileText

        public static String getFileText​(Path p_file)
                                  throws IOException
        Get the content of a small UTF-8 encoded text file.

        Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading in large files.

        Parameters:
        p_file - The text file.
        Returns:
        The content of the given text file.
        Throws:
        FileNotFoundException - If the file does not exist.
        IOException - If an I/O error occurred.
        OutOfMemoryError - If the file is too large to be read into memory.
        Since:
        Intrexx 9.0.
      • getFileText

        public static String getFileText​(Path p_file,
                                         Charset p_encoding)
                                  throws IOException
        Get the content of a small text file.

        Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading in large files.

        Parameters:
        p_file - The text file.
        p_encoding - The file encoding.
        Returns:
        The content of the given text file.
        Throws:
        FileNotFoundException - If the file does not exist.
        IOException - If an I/O error occurred.
        OutOfMemoryError - If the file is too large to be read into memory.
        Since:
        Intrexx 9.0.
      • copyDir

        public static void copyDir​(Path p_dirDest,
                                   Path p_dirSrc)
                            throws IOException
        Copy a directory recursively. p_dest is the destination directory. If this directory does not exist, it will be created. The content of the source directory will be copied into it.
        Parameters:
        p_dirDest - The destination directory.
        p_dirSrc - The directory to copy.
        Throws:
        IOException - If an I/O error occurred.
        Since:
        Intrexx 8.0.
      • copyDir

        public static void copyDir​(Path p_dirDest,
                                   Path p_dirSrc,
                                   boolean p_bCopyMarkedDirs)
                            throws IOException
        Copy a directory recursively. p_dest is the destination directory. If this directory does not exist, it will be created. The content of the source directory will be copied into it.
        Parameters:
        p_dirDest - The destination directory.
        p_dirSrc - The directory to copy.
        p_bCopyMarkedDirs - copy directories, event if they are marked with a ".noexport" file. If this parameter is set to false, the destination directory is created but left empty.
        Throws:
        IOException - If an I/O error occurs.
        Since:
        Intrexx 8.0.
      • copyDir

        public static void copyDir​(Path p_dirDest,
                                   Path p_dirSrc,
                                   DirectoryStream.Filter<Path> p_filter)
                            throws IOException
        Copy a directory recursively. Copy only the files matching the specified file filter.
        p_dest is the parent directory of the new directory.
        Parameters:
        p_dirDest - The destination directory.
        p_dirSrc - The directory to copy.
        p_filter - File filter that filters the files that will be copied. If null is specified, no filter applies.
        Throws:
        IOException - If an I/O error occurred.
        Since:
        Intrexx 8.0.
      • copyDir

        public static void copyDir​(Path p_dirDest,
                                   Path p_dirSrc,
                                   DirectoryStream.Filter<Path> p_filter,
                                   boolean p_bCopyMarkedDirs)
                            throws IOException
        Copy a directory recursively. Copy only the files matching the specified file filter.
        p_dest is the parent directory of the new directory.
        Parameters:
        p_dirDest - The destination directory.
        p_dirSrc - The directory to copy.
        p_filter - File filter that filters the files that will be copied. If null is specified, no filter applies.
        p_bCopyMarkedDirs - copy directories, event if they are marked with a ".noexport" file. If this parameter is set to false, the destination directory is created with a .noxexport file as the only content.
        Throws:
        IOException - If an I/O error occurred.
        Since:
        Intrexx 8.0.
      • delete

        public static void delete​(Path p_path)
                           throws IOException
        Deletes a directory tree recursively. If the specified path points to a file, this method deletes the files. If it is a directory, all items of the tree are deleted as well as the specified directory.
        Parameters:
        p_path - The directory path to delete.
        Throws:
        IOException - If an I/O error occurred.
        Since:
        Intrexx 8.0.
      • isPathBelow

        public static boolean isPathBelow​(Path p_dir,
                                          Path p_path)
                                   throws NoSuchFileException,
                                          IOException
        Check if the given file's or directory's location is below the given existing directory. The file or directory given as the second parameter is not required to exist.

        If the given path is relative it is assumed to be relative to the current working directory.

        Parameters:
        p_dir - The containing directory (must exist physically).
        p_path - The path to test (is not required to exist physically).
        Returns:
        true if the given file's location is below the given, directory, or false otherwise.
        Throws:
        NoSuchFileException - If the containing directory does not exist.
        IOException - If an I/O error occurred.
        IllegalArgumentException - If the given directory or path is null.
        Since:
        Intrexx 9.0.
      • getPathBelow

        public static Path getPathBelow​(String p_strPath)
        Get a file object that represents a file or directory in a sub-path below the Java VM's working directory.
        Parameters:
        p_strPath - The sub-path within the current working directory.
        Returns:
        A path object that represents a file or directory in a sub-path of the current working directory.
        Throws:
        IllegalArgumentException - If the given path is null or empty.
        SecurityException - If the given path references a file location outside the given directory.
        Since:
        Intrexx 9.0.
      • getPathBelow

        public static Path getPathBelow​(Path p_dir,
                                        String... p_subs)
        Get a file object that represents a file or directory in a sub-path below a given directory.
        Parameters:
        p_dir - The base directory where the given sub-path should be rooted.
        p_subs - The sub-path within the given directory.
        Returns:
        A file object that represents a file or directory in a sub-path of the given directory.
        Throws:
        IllegalArgumentException - If p_dir is null, or if the directory p_dir does not exist, or if p_subs is null or empty or contains null or empty path elements.
        SecurityException - If p_path references a file location outside the given directory.
        Since:
        Intrexx 9.0.
      • getUniqueNumberedFile

        public static Path getUniqueNumberedFile​(Path p_file)
        Returns:
        A file path base on the given file that does not exist.
        Since:
        Intrexx 9.0.
      • stripFileNumber

        public static Path stripFileNumber​(Path p_file)
        Returns:
        The file with removed trailing file number.
        Since:
        Intrexx 9.0.
      • createBackupFile

        public static Path createBackupFile​(Path p_file,
                                            Path p_dir)
                                     throws IOException
        Create a backup of the given file.

        The backuo file has the same name as the original file with the suffix .yyyy-MM-dd-HH-mm-ss.bak containing a timestamp in the default time zone appended.

        Parameters:
        p_file - The file to backup.
        Returns:
        The created backup file.
        Throws:
        IOException - If an I/O error occurred.
        Since:
        Intrexx 9.0.