Package com._1c.g5.v8.dt.common
Class FileUtil
- java.lang.Object
-
- com._1c.g5.v8.dt.common.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 StringDT_TEMP_FOLDERCommon DT IDE parent folder name to use for temporary folder creation.static charUTF_BOMUnicode Byte Order Mark (BOM) char, value is.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanFolderIfEmpty(org.eclipse.core.resources.IContainer container)Removes all container folders if they are empty start from givencontainer.static voidcopyRecursively(Path sourcePath, Path targetPath)Copies folder fromsourcePathwith its sub folders totargetPathstatic voidcreateEmptyFileIfNotExist(org.eclipse.core.resources.IFile file)Create empty file if it is not already exist.static voidcreateFolders(org.eclipse.core.resources.IFolder folder)Creates a folder by creating all nonexistent parent folders first.static voidcreateParentFolders(org.eclipse.core.resources.IFile file)Creates parent folders recursively for given file.static FilecreateTempDirectory()Creates a new directory in the DT IDE temporary directory, using generated name.static FilecreateTempDirectory(String prefix)Creates a new directory in the DT IDE temporary directory, using the givenprefixto generate its name.static FilecreateTempDirectory(String prefix, Path parent)Creates a new directory in the DT IDE temporary directory, using the givenprefixto generate its name under the provided parent directory.static FilecreateTempFile(String prefix, String suffix)Creates a new empty file in the DT IDE temporary directory, using the givenprefixandsuffixstrings to generate its name.static voiddeleteRecursively(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 voiddeleteRecursively(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 voiddeleteRecursivelyWithRetries(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 StringgetExtension(File file)Returns file extension of the given file, resolved by the file name.static StringgetExtension(String fileName)Returns file extension of the given file, resolved by the file name.static PathgetTempBasePath()Returns DT IDE base path for temporary directories and files location.static booleanhasExtension(File file, String extension)Returns whether the given file has the given file extension.static booleanisEmpty(Path path)Checks that file is emptystatic voidmoveOrRenameFile(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 emptystatic voidmoveOrRenameFolder(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 emptystatic BufferedReaderskipBom(BufferedReader reader)Skip Unicode BOM in the given buffered reader.static StringtrimExtension(File file)Returns the file name with trimmed extension of the file.static StringtrimExtension(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 givenprefixandsuffixstrings to generate its name.Instead of
com.google.common.io.Filesandjava.nio.file.Filescreates temp file under generalDT_TEMP_FOLDERsuper directory.- Parameters:
prefix- the prefix string to be used in generating the file's name ornullif not neededsuffix- the suffix string to be used in generating the file's name ornullif not needed, in which case.tmpis 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.Filesandjava.nio.file.Filescreates temp directory under generalDT_TEMP_FOLDERsuper 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 givenprefixto generate its name.Instead of
com.google.common.io.Filesandjava.nio.file.Filescreates temp directory under generalDT_TEMP_FOLDERsuper directory.- Parameters:
prefix- the prefix string to be used in generating the directory's name ornullif 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 givenprefixto generate its name under the provided parent directory.- Parameters:
prefix- the prefix string to be used in generating the directory's name ornullif not neededparent- the parent directory to create in, cannot benull- 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 benull- Throws:
IOException- if delete fails for some reason
-
deleteRecursively
public static final void deleteRecursively(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.- Parameters:
path- the path of the file or folder to delete recursively, cannot benull- 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 benull- 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 benull- Returns:
- file extension of the given file or empty
Stringif 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 benull- Returns:
- file extension of the given file or empty
Stringif 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 benullextension- the file extension to check, cannot benull- 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 benull- 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 benull- 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 benull- 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.CoreExceptionRemoves all container folders if they are empty start from givencontainer. If container isIProjectthen removal process stops.- Parameters:
container- container to start cleaning from (notnull)- 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.CoreExceptionCreates 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
IResourceChangeEventfor more details.
-
createFolders
public static void createFolders(org.eclipse.core.resources.IFolder folder) throws org.eclipse.core.runtime.CoreExceptionCreates a folder by creating all nonexistent parent folders first.- Parameters:
folder- The folder to create. May not benull.- 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
IResourceChangeEventfor 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.CoreExceptionMoves 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 benulloldFile- The old file path. May not benull- 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
IResourceChangeEventfor 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.CoreExceptionMoves 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 benulloldFolder- The old folder path. May not benull- 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
IResourceChangeEventfor more details.
-
createEmptyFileIfNotExist
public static final void createEmptyFileIfNotExist(org.eclipse.core.resources.IFile file) throws org.eclipse.core.runtime.CoreExceptionCreate 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
IResourceChangeEventfor more details.
-
copyRecursively
public static void copyRecursively(Path sourcePath, Path targetPath) throws IOException
Copies folder fromsourcePathwith its sub folders totargetPath- Parameters:
sourcePath- path to the copying folder, can't benulltargetPath- path to the destination of copied folder, can't benull- Throws:
IOException- ifIOExceptionwas occurred, seeFiles.walkFileTree(Path, java.nio.file.FileVisitor)
-
isEmpty
public static boolean isEmpty(Path path) throws IOException
Checks that file is empty- Parameters:
path- path to the checked file, can't benull- Returns:
trueif the file content is empty or there is no such files,falseotherwise- Throws:
IOException- ifIOExceptionwas occurred
-
-