Package com._1c.g5.v8.dt.common
Class FileUtil
java.lang.Object
com._1c.g5.v8.dt.common.FileUtil
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Automatically deletes folder (or file) when leaving try-with-resources block. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Common DT IDE parent folder name to use for temporary folder creation.static final char
Unicode Byte Order Mark (BOM) char, value is
. -
Method Summary
Modifier and TypeMethodDescriptionstatic final void
cleanFolderIfEmpty
(org.eclipse.core.resources.IContainer container) Removes all container folders if they are empty start from givencontainer
.static void
copyRecursively
(Path sourcePath, Path targetPath) Copies folder fromsourcePath
with its sub folders totargetPath
static final void
createEmptyFileIfNotExist
(org.eclipse.core.resources.IFile file) Create empty file if it is not already exist.static void
createFolders
(org.eclipse.core.resources.IFolder folder) Creates a folder by creating all nonexistent parent folders first.static final void
createParentFolders
(org.eclipse.core.resources.IFile file) Creates parent folders recursively for given file.static File
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 givenprefix
to generate its name.static File
createTempDirectory
(String prefix, Path parent) Creates a new directory in the DT IDE temporary directory, using the givenprefix
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 givenprefix
andsuffix
strings to generate its name.static final 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 final void
deleteRecursively
(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 final void
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 final String
getExtension
(File file) Returns file extension of the given file, resolved by the file name.static final String
getExtension
(String fileName) Returns file extension of the given file, resolved by the file name.static Collection<Path>
getFilesInDirectory
(Path directory, Predicate<? super Path> filter) Provides the list of files in selected directorystatic Path
Returns DT IDE base path for temporary directories and files location.static final boolean
hasExtension
(File file, String extension) Returns whether the given file has the given file extension.static boolean
Checks that file is emptystatic final 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 emptystatic final 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 emptystatic final BufferedReader
skipBom
(BufferedReader reader) Skip Unicode BOM in the given buffered reader.static final String
trimExtension
(File file) Returns the file name with trimmed extension of the file.static final String
trimExtension
(Path path) Returns the file name with trimmed extension of the path.
-
Field Details
-
UTF_BOM
public static final char UTF_BOMUnicode Byte Order Mark (BOM) char, value is
.- See Also:
-
ContentHandler.ByteOrderMark
IContentDescription
- Constant Field Values
-
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:
-
-
Method Details
-
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:
-
createTempFile
Creates a new empty file in the DT IDE temporary directory, using the givenprefix
andsuffix
strings to generate its name.Instead of
com.google.common.io.Files
andjava.nio.file.Files
creates temp file under generalDT_TEMP_FOLDER
super directory.- Parameters:
prefix
- the prefix string to be used in generating the file's name ornull
if not neededsuffix
- the suffix string to be used in generating the file's name ornull
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
Creates a new directory in the DT IDE temporary directory, using generated name.Instead of
com.google.common.io.Files
andjava.nio.file.Files
creates temp directory under generalDT_TEMP_FOLDER
super directory.- Returns:
- the creted temporary directory, never
null
- Throws:
IOException
- if an I/O error occurs
-
createTempDirectory
Creates a new directory in the DT IDE temporary directory, using the givenprefix
to generate its name.Instead of
com.google.common.io.Files
andjava.nio.file.Files
creates temp directory under generalDT_TEMP_FOLDER
super directory.- Parameters:
prefix
- the prefix string to be used in generating the directory's name ornull
if not needed- Returns:
- the creted temporary directory, never
null
- Throws:
IOException
- if an I/O error occurs
-
createTempDirectory
Creates a new directory in the DT IDE temporary directory, using the givenprefix
to generate its name under the provided parent directory.- Parameters:
prefix
- the prefix string to be used in generating the directory's name ornull
if 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
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
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
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
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
String
if cannot get
-
getExtension
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
String
if cannot get
-
hasExtension
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 benull
extension
- the file extension to check, cannot benull
- Returns:
- whether the given file has the given file extension
-
getFilesInDirectory
public static Collection<Path> getFilesInDirectory(Path directory, Predicate<? super Path> filter) throws IOException Provides the list of files in selected directory- Parameters:
directory
- the target directory, cannot benull
filter
- the files filter, can benull
- Returns:
- the list of files in selected directory, never
null
- Throws:
IOException
-
trimExtension
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
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
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.CoreException Removes all container folders if they are empty start from givencontainer
. If container isIProject
then 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.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.
-
createFolders
public static void createFolders(org.eclipse.core.resources.IFolder folder) throws org.eclipse.core.runtime.CoreException Creates 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
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 benull
oldFile
- 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
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 benull
oldFolder
- 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
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.
-
copyRecursively
Copies folder fromsourcePath
with its sub folders totargetPath
- Parameters:
sourcePath
- path to the copying folder, can't benull
targetPath
- path to the destination of copied folder, can't benull
- Throws:
IOException
- ifIOException
was occurred, seeFiles.walkFileTree(Path, java.nio.file.FileVisitor)
-
isEmpty
Checks that file is empty- Parameters:
path
- path to the checked file, can't benull
- Returns:
true
if the file content is empty or there is no such files,false
otherwise- Throws:
IOException
- ifIOException
was occurred
-