Class FileUtil


  • public final class FileUtil
    extends Object
    File utility methods.
    Restriction:
    This class is not intended to be sub-classed by clients.
    Restriction:
    This class is not intended to be instantiated by clients.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String DT_TEMP_FOLDER
      Common DT IDE parent folder name to use for temporary folder creation.
      static char UTF_BOM
      Unicode Byte Order Mark (BOM) char, value is .
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void cleanFolderIfEmpty​(org.eclipse.core.resources.IContainer container)
      Removes all container folders if they are empty start from given container.
      static void copyRecursively​(Path sourcePath, Path targetPath)
      Copies folder from sourcePath with its sub folders to targetPath
      static void createEmptyFileIfNotExist​(org.eclipse.core.resources.IFile file)
      Create empty file if it is not already exist.
      static void createParentFolders​(org.eclipse.core.resources.IFile file)
      Creates parent folders recursively for given file.
      static File createTempDirectory()
      Creates a new directory in the DT IDE temporary directory, using generated name.
      static File createTempDirectory​(String prefix)
      Creates a new directory in the DT IDE temporary directory, using the given prefix to generate its name.
      static File createTempDirectory​(String prefix, Path parent)
      Creates a new directory in the DT IDE temporary directory, using the given prefix to generate its name under the provided parent directory.
      static File createTempFile​(String prefix, String suffix)
      Creates a new empty file in the DT IDE temporary directory, using the given prefix and suffix strings to generate its name.
      static void deleteRecursively​(File file)
      Delete the target file, if it is a file, or delete the target folder recursively with entire content of files and sub-folders if it is a folder.
      static void deleteRecursivelyWithRetries​(Path path)
      Delete the target file, if it is a file, or delete the target folder recursively with entire content of files and sub-folders if it is a folder.
      static String getExtension​(File file)
      Returns file extension of the given file, resolved by the file name.
      static String getExtension​(String fileName)
      Returns file extension of the given file, resolved by the file name.
      static Path getTempBasePath()
      Returns DT IDE base path for temporary directories and files location.
      static boolean hasExtension​(File file, String extension)
      Returns whether the given file has the given file extension.
      static boolean isEmpty​(Path path)
      Checks that file is empty
      static void moveOrRenameFile​(org.eclipse.core.resources.IFile newFile, org.eclipse.core.resources.IFile oldFile)
      Moves or renames the file Supports renaming of separate parts of the path, supports renames with case-changes only Supports creation of missed part of folders
      Cleans old folder if empty
      static void moveOrRenameFolder​(org.eclipse.core.resources.IFolder newFolder, org.eclipse.core.resources.IFolder oldFolder)
      Moves or renames the folder Supports renaming of separate parts of the path, supports renames with case-changes only Supports creation of missed part of folders
      Cleans old folder if empty
      static BufferedReader skipBom​(BufferedReader reader)
      Skip Unicode BOM in the given buffered reader.
      static String trimExtension​(File file)
      Returns the file name with trimmed extension of the file.
      static String trimExtension​(Path path)
      Returns the file name with trimmed extension of the path.
    • Field Detail

      • UTF_BOM

        public static final char UTF_BOM
        Unicode Byte Order Mark (BOM) char, value is .
        See Also:
        ContentHandler.ByteOrderMark, IContentDescription, Constant Field Values
      • DT_TEMP_FOLDER

        public static final String DT_TEMP_FOLDER
        Common DT IDE parent folder name to use for temporary folder creation. Used to hold DT IDE temp folders in one parent folder.
        See Also:
        Constant Field Values
    • Method Detail

      • getTempBasePath

        public static Path getTempBasePath()
        Returns DT IDE base path for temporary directories and files location.
        Returns:
        DT IDE base path for temporary directories and files location, never null
        See Also:
        DT_TEMP_FOLDER
      • createTempFile

        public static File createTempFile​(String prefix,
                                          String suffix)
                                   throws IOException
        Creates a new empty file in the DT IDE temporary directory, using the given prefix and suffix strings to generate its name.

        Instead of com.google.common.io.Files and java.nio.file.Files creates temp file under general DT_TEMP_FOLDER super directory.

        Parameters:
        prefix - the prefix string to be used in generating the file's name or null if not needed
        suffix - the suffix string to be used in generating the file's name or null if not needed, in which case .tmp is used
        Returns:
        the creted temporary file, never null
        Throws:
        IOException - if an I/O error occurs
      • createTempDirectory

        public static File createTempDirectory()
                                        throws IOException
        Creates a new directory in the DT IDE temporary directory, using generated name.

        Instead of com.google.common.io.Files and java.nio.file.Files creates temp directory under general DT_TEMP_FOLDER super directory.

        Returns:
        the creted temporary directory, never null
        Throws:
        IOException - if an I/O error occurs
      • createTempDirectory

        public static File createTempDirectory​(String prefix)
                                        throws IOException
        Creates a new directory in the DT IDE temporary directory, using the given prefix to generate its name.

        Instead of com.google.common.io.Files and java.nio.file.Files creates temp directory under general DT_TEMP_FOLDER super directory.

        Parameters:
        prefix - the prefix string to be used in generating the directory's name or null if not needed
        Returns:
        the creted temporary directory, never null
        Throws:
        IOException - if an I/O error occurs
      • createTempDirectory

        public static File createTempDirectory​(String prefix,
                                               Path parent)
                                        throws IOException
        Creates a new directory in the DT IDE temporary directory, using the given prefix to generate its name under the provided parent directory.
        Parameters:
        prefix - the prefix string to be used in generating the directory's name or null if not needed
        parent - the parent directory to create in, cannot be null
        Returns:
        the creted temporary directory, never null
        Throws:
        IOException - if an I/O error occurs
      • deleteRecursively

        public static final void deleteRecursively​(File file)
                                            throws IOException
        Delete the target file, if it is a file, or delete the target folder recursively with entire content of files and sub-folders if it is a folder.
        Parameters:
        file - the file or folder to delete recursively, cannot be null
        Throws:
        IOException - if delete fails for some reason
      • deleteRecursivelyWithRetries

        public static final void deleteRecursivelyWithRetries​(Path path)
                                                       throws IOException
        Delete the target file, if it is a file, or delete the target folder recursively with entire content of files and sub-folders if it is a folder. If an I/O error occurs, deletion is retried.

        This method is intended to bypass problems with anti-virus software on Windows. Unike Unix, Windows does not allow to delete a file if it is open, thus inspections performed by anti-virus software cause deletion to fail.

        Parameters:
        path - The path of the file or folder to delete, cannot be null
        Throws:
        IOException - if an I/O error occurs and no more retries left.
      • getExtension

        public static final String getExtension​(File file)
        Returns file extension of the given file, resolved by the file name.
        Parameters:
        file - the file to get extension for, cannot be null
        Returns:
        file extension of the given file or empty String if cannot get
      • getExtension

        public static final String getExtension​(String fileName)
        Returns file extension of the given file, resolved by the file name.
        Parameters:
        file - the file to get extension for, cannot be null
        Returns:
        file extension of the given file or empty String if cannot get
      • hasExtension

        public static final boolean hasExtension​(File file,
                                                 String extension)
        Returns whether the given file has the given file extension. File extension is resolved by the file name.
        Parameters:
        file - the file to check extension for, cannot be null
        extension - the file extension to check, cannot be null
        Returns:
        whether the given file has the given file extension
      • trimExtension

        public static final String trimExtension​(File file)
        Returns the file name with trimmed extension of the file.
        Parameters:
        file - the file to trim extension for, cannot be null
        Returns:
        the file name with trimmed extension of the file or the original file name if cannot get file extension
      • trimExtension

        public static final String trimExtension​(Path path)
        Returns the file name with trimmed extension of the path.
        Parameters:
        path - the file path to trim extension for, cannot be null
        Returns:
        the file name with trimmed extension of the path or the original file or dirictory name if cannot get extension
      • skipBom

        public static final BufferedReader skipBom​(BufferedReader reader)
                                            throws IOException
        Skip Unicode BOM in the given buffered reader. If reader has no BOM in the current read cursor then reader will stay unchanged.
        Parameters:
        reader - the reader to skip Unicode BOM, cannot be null
        Returns:
        a reference to the using reader, never null
        Throws:
        IOException - if I/0 error occurred
      • cleanFolderIfEmpty

        public static final void cleanFolderIfEmpty​(org.eclipse.core.resources.IContainer container)
                                             throws org.eclipse.core.runtime.CoreException
        Removes all container folders if they are empty start from given container. If container is IProject then removal process stops.
        Parameters:
        container - container to start cleaning from (not null)
        Throws:
        org.eclipse.core.runtime.CoreException - if this request fails. Reasons include:
      • This resource does not exist.
      • This resource is a project that is not open.
      • createParentFolders

        public static final void createParentFolders​(org.eclipse.core.resources.IFile file)
                                              throws org.eclipse.core.runtime.CoreException
        Creates parent folders recursively for given file.
        Parameters:
        file - the file to create parent folders for
        Throws:
        org.eclipse.core.runtime.CoreException - if this method fails. Reasons include:
        • The workspace contains a resource of a different type at the same path as this resource.
        • The parent of this resource or containing folders is a project that is not open.
        • The parent contains a resource of a different type at the same path as this resource.
        • The parent of this resource is virtual, but this resource is not.
        • The name of one of folders is not valid (according to IWorkspace.validateName).
        • The corresponding location in the local file system is occupied by a file (as opposed to a directory).
        • Resource changes are disallowed during certain types of resource change event notification. See IResourceChangeEvent for more details.
      • moveOrRenameFile

        public static final void moveOrRenameFile​(org.eclipse.core.resources.IFile newFile,
                                                  org.eclipse.core.resources.IFile oldFile)
                                           throws org.eclipse.core.runtime.CoreException
        Moves or renames the file Supports renaming of separate parts of the path, supports renames with case-changes only Supports creation of missed part of folders
        Cleans old folder if empty
        Parameters:
        newFile - The new file path. May not be null
        oldFile - The old file path. May not be null
        Throws:
        org.eclipse.core.runtime.CoreException - if this method fails. Reasons include:
        • The workspace contains a resource of a different type at the same path as this resource.
        • The parent of this resource or containing folders is a project that is not open.
        • The parent contains a resource of a different type at the same path as this resource.
        • The parent of this resource is virtual, but this resource is not.
        • The name of one of folders is not valid (according to IWorkspace.validateName).
        • The corresponding location in the local file system is occupied by a file (as opposed to a directory).
        • Resource changes are disallowed during certain types of resource change event notification. See IResourceChangeEvent for more details.
      • moveOrRenameFolder

        public static final void moveOrRenameFolder​(org.eclipse.core.resources.IFolder newFolder,
                                                    org.eclipse.core.resources.IFolder oldFolder)
                                             throws org.eclipse.core.runtime.CoreException
        Moves or renames the folder Supports renaming of separate parts of the path, supports renames with case-changes only Supports creation of missed part of folders
        Cleans old folder if empty
        Parameters:
        newFolder - The new folder path. May not be null
        oldFolder - The old folder path. May not be null
        Throws:
        org.eclipse.core.runtime.CoreException - if this method fails. Reasons include:
        • The workspace contains a resource of a different type at the same path as this resource.
        • The parent of this resource or containing folders is a project that is not open.
        • The parent contains a resource of a different type at the same path as this resource.
        • The parent of this resource is virtual, but this resource is not.
        • The name of one of folders is not valid (according to IWorkspace.validateName).
        • The corresponding location in the local file system is occupied by a file (as opposed to a directory).
        • Resource changes are disallowed during certain types of resource change event notification. See IResourceChangeEvent for more details.
      • createEmptyFileIfNotExist

        public static final void createEmptyFileIfNotExist​(org.eclipse.core.resources.IFile file)
                                                    throws org.eclipse.core.runtime.CoreException
        Create empty file if it is not already exist. Creates parent folders recursively for given file.
        Parameters:
        file - the file to create
        Throws:
        org.eclipse.core.runtime.CoreException - if this method fails. Reasons include:
        • The workspace contains a resource of a different type at the same path as this resource.
        • The parent of this resource or containing folders is a project that is not open.
        • The parent contains a resource of a different type at the same path as this resource.
        • The parent of this resource is virtual, but this resource is not.
        • The name of one of folders is not valid (according to IWorkspace.validateName).
        • The corresponding location in the local file system is occupied by a file (as opposed to a directory).
        • Resource changes are disallowed during certain types of resource change event notification. See IResourceChangeEvent for more details.
      • isEmpty

        public static boolean isEmpty​(Path path)
                               throws IOException
        Checks that file is empty
        Parameters:
        path - path to the checked file, can't be null
        Returns:
        true if the file content is empty or there is no such files, false otherwise
        Throws:
        IOException - if IOException was occurred