jetbrains.buildServer.util
Class FileUtil

java.lang.Object
  extended by com.intellij.openapi.util.io.FileUtil
      extended by jetbrains.buildServer.util.FileUtil

public class FileUtil
extends com.intellij.openapi.util.io.FileUtil

Since:
4.0
Author:
Kir

Nested Class Summary
static interface FileUtil.Processor
           
static class FileUtil.RegexFileFilter
          File filter that accepts files with the name specified by a regular expression.
 
Field Summary
 
Fields inherited from class com.intellij.openapi.util.io.FileUtil
MEGABYTE
 
Constructor Summary
FileUtil()
           
 
Method Summary
static boolean areTheSame(java.io.File file1, java.io.File file2)
          Checks that both file pointers specify the same object.
static boolean checkContentEqual(java.io.File file1, java.io.File file2)
          Check both files exist and are same byte-by-byte.
static void checkTmpDir()
          Checks if it is allowed to use tmpDir taken from "java.io.tmpdir" system property
static void close(java.io.Closeable e)
          Closes a resource if it is not null All possible IOExceptions are ignored
static void close(java.util.zip.ZipFile e)
          Closes a resource if it is not null All possible IOExceptions are ignored
static void closeAll(java.io.Closeable... toClose)
          Closes all not-null resources All possible IOExceptions are ignored
static java.lang.String convertAntToRegexp(java.lang.String antPattern)
           
static java.lang.String convertAntToRegexp(java.lang.String antPattern, boolean ignoreStartingSlash)
           
static void copy(java.io.File from, java.io.File to)
           
static void copy(java.io.InputStream is, java.io.OutputStream os)
          Same as copyStream.
static void copyFilePart(java.io.File sourceFile, long offset, long length, java.io.OutputStream destination)
          Copies a part of a file to output stream.
static void copyResource(java.lang.Class<?> clazz, java.lang.String resourceName, java.io.File outputFile)
          Copies contents of the specified resource into the specified file.
static void copyResource(java.lang.String resourceName, java.io.File outputFile)
          Deprecated. Use copyResource(Class, String, java.io.File) to overcome classloader change issues
static void copyResourceIfNotExists(java.lang.Class<?> clazz, java.lang.String resourceName, java.io.File outputFile)
          Copies the resource file into specified file if it doesn't exist (see copyResource(Class, String, java.io.File)).
static void copyResourceWithDist(java.lang.Class<?> clazz, java.lang.String resourceName, java.io.File outputFile)
          Copies the resource file into specified file (see copyResource(Class, String, java.io.File)).
static void copyStreamRangeAndClose(java.io.InputStream source, long offset, long length, java.io.OutputStream destination)
          Copies a stream range of a file to output stream.
static void copyStreams(java.io.InputStream is, java.io.OutputStream os)
           
static java.io.File createDir(java.io.File dir)
          Creates directory if it does not exist.
static java.io.File createEmptyDir(java.io.File dir)
          Creates an empty directory, removing all content if it already exists.
static java.io.File createTempDirectory(java.lang.String prefix, java.lang.String suffix, java.io.File in)
           
static boolean delete(java.io.File file)
          Removes specified file or directory, same as FileUtil.delete(file, 10).
static java.util.Set<java.io.File> delete(java.io.File fileOrDirectory, java.io.FileFilter fileFilter, int numAttempts)
          Removes specified directory or file, for each removed file performs operation up to numAttempts
static java.util.Set<java.io.File> delete(java.io.File fileOrDirectory, int numAttempts)
          Removes specified directory or file, for each removed file performs operation up to numAttempts.
static void deleteIfEmpty(java.io.File dir)
          Removes directory if it is empty
static java.io.File findFile(java.io.FileFilter filter, java.io.File... directories)
          Searches for a file in the specified directories recursively.
static java.util.Collection<java.io.File> findFiles(java.io.FileFilter filter, java.io.File... directories)
          Searches for files in the specified directories recursively.
static java.lang.String fixDirectoryName(java.lang.String name)
          Fixes directory name: replaces all dangerous characters with '_'.
static java.io.File getCanonicalFile(java.io.File file)
          Attempts to obtain canonical file from the specified file and if it fails returns absolute file.
static java.lang.String getCommonParent(java.lang.String firstUrl, java.lang.String secondUrl)
           
static java.lang.String getExtension(java.lang.String fileNameWithDirectory)
          Unlike com.intellij.openapi.util.io.FileUtil.getExtension, can use this with full full file name (including directory name).
static java.io.File getFileInSameDir(java.io.File origFile, java.lang.String neededFileName)
          Returns a java.io.File object representing the file located in the same directory as origFile with the given file name

assert getFileInSameDir(new File("somDir/someFile.txt"), "anotherFile.txt").
static java.lang.String getFilesSignature(java.util.Collection<java.io.File> _files)
           
static java.lang.Long getFreeSpace(java.io.File file)
          Computes free space on the disk that contains a file
static java.lang.String getNameWithoutExtension(java.io.File file)
           
static java.lang.String getNameWithoutExtension(java.lang.String name)
           
static java.util.List<java.io.File> getSubDirectories(java.io.File root)
           
static long getTotalDirectorySize(java.io.File dir)
          Returns the directory size in bytes of files under the specified directory (non recursive).
static long getTotalDirectorySize(java.io.File dir, boolean recursively)
          Returns the directory size in bytes of files under the specified directory recursively.
static boolean isAncestor(java.io.File ancestor, java.io.File file, boolean strict)
          Check if the ancestor is an ancestor of file.
static boolean isBadDirectoryName(java.lang.String name)
          Checks whether specified name contains dangerous characters which might prevent successful directory creation.
static boolean isBinaryData(byte[] data, int size)
          Returns whether specified byte array represents binary data (not text)
static boolean isBinaryFile(java.io.File file)
          Returns whether a specified file is a binary file.
static boolean isEmptyDir(java.io.File dir)
          Returns true if directory is empty or does not exist and false otherwise.
static boolean isFileSeparator(char c1)
           
static boolean isReadonly(java.io.File file)
          Returns true if file is readonly and false otherwise
static boolean isTextData(byte[] data, int size)
          Returns whether specified byte array represents text data (not binary)
static java.io.File[] listFiles(java.io.File file, java.io.FilenameFilter filter)
          A simple wrapper around file.listFiles(filter) that handles possible null result.
static
<C extends java.util.Collection<java.lang.String>>
C
listFilesRecursively(java.io.File dir, java.lang.String prefix, boolean includeDirs, int depthLimit, Predicate<java.io.File> predicate, C collection)
          Traverses the given directory and retrieves all files (and nested directories, if specified).
static java.lang.String normalizeRelativePath(java.lang.String path)
          Normalizes relative path by eliminating all possible path/../ /./ ./path combinations
static java.lang.String normalizeSeparator(java.lang.String filePath)
          Replace all separators like / and \ to File.separatorChar
static org.jdom.Element parseDocument(java.io.File file)
           
static org.jdom.Element parseDocument(java.io.File file, boolean validate)
           
static org.jdom.Element parseDocument(java.io.InputStream input, boolean validate)
           
static org.jdom.Element parseDocument(java.io.Reader input, boolean validate)
           
static void processXmlFile(java.io.File file, FileUtil.Processor p)
           
static java.util.List<java.lang.String> readFile(java.io.File file)
          Read list of lines from the file.
static java.lang.String readResourceAsString(java.lang.Class<?> clazz, java.lang.String resourceName, java.nio.charset.Charset charset)
          Reads the jar resource as a text.
static java.lang.String readText(java.io.File file)
          Read the specified text file into a String variable.
static java.lang.String readText(java.io.File file, java.lang.String encoding)
          Read the specified text file into a String variable.
static void readXmlFile(java.io.File file, FileUtil.Processor p)
           
static void renameAndReportErrors(java.io.File oldName, java.io.File newName)
          Renames or moves the give file or directory.
static java.io.File renameFileNameOnly(java.io.File file, java.lang.String newName)
           
static java.io.File resolvePath(java.io.File baseDir, java.lang.String path)
          If path is absolute, returns path, otherwise it returns path within specified base directory.
static boolean sameButDifferInCase(java.io.File file1, java.io.File file2)
          Check whether the two given paths denotes the same file but in differ cases.
static void saveDocument(org.jdom.Document document, java.io.File file)
           
static void setExectuableAttribute(java.lang.String path, boolean executableFlag)
          Set executable attribute, it makes sense only on non-windows platforms.
static void sortFiles(java.io.File[] files)
           
static void writeFile(java.io.File file, java.lang.String text)
          Deprecated. This procedure loses exceptions that is very dangerous. Use writeFileAndReportErrors(java.io.File, java.lang.String) instead.
static void writeFile(java.io.File file, java.lang.String text, java.lang.String encoding)
          Writes text to file.
static void writeFileAndReportErrors(java.io.File file, java.lang.String text)
          Writes text to file.
 
Methods inherited from class com.intellij.openapi.util.io.FileUtil
adaptiveLoadBytes, adaptiveLoadText, asyncDelete, asyncDelete, canCallCanExecute, canExecute, collectMatchedFiles, comparePaths, copy, copyContent, copyDir, copyDir, copyDir, createIfDoesntExist, createParentDirs, createSequentFileName, createTempDirectory, createTempFile, createTempFile, ensureCanCreateFile, findFirstThatExist, findSequentNonexistentFile, getParentFile, getRelativePath, getRelativePath, getRelativePath, getTempDirectory, isAbsolute, isFilePathAcceptable, loadBytes, loadBytes, loadFileBytes, loadFileText, loadFileText, loadText, loadTextAndClose, moveDirWithContent, nameToCompare, pathHashCode, pathsEqual, processFilesRecursively, rename, resolveShortWindowsName, sanitizeFileName, setReadOnlyAttribute, startsWith, startsWith, toSystemDependentName, toSystemIndependentName, unquote, writeToFile, writeToFile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

readFile

@NotNull
public static java.util.List<java.lang.String> readFile(@NotNull
                                                                java.io.File file)
                                                 throws java.io.IOException
Read list of lines from the file. Each line is terminated by corresponding line feed

Parameters:
file - file to read
Returns:
list of file lines
Throws:
java.io.IOException - upon a problem when reading from file
Since:
4.0.1

readText

public static java.lang.String readText(@NotNull
                                        java.io.File file)
                                 throws java.io.IOException
Read the specified text file into a String variable.

Parameters:
file - file to read
Returns:
file text
Throws:
java.io.IOException - upon a problem when reading from file
Since:
5.0

readText

public static java.lang.String readText(@NotNull
                                        java.io.File file,
                                        @NotNull
                                        java.lang.String encoding)
                                 throws java.io.IOException
Read the specified text file into a String variable.

Parameters:
file - file to read
encoding - encoding
Returns:
file text
Throws:
java.io.IOException - upon a problem when reading from file
Since:
7.1

writeFile

public static void writeFile(@NotNull
                             java.io.File file,
                             @NotNull
                             java.lang.String text)
Deprecated. This procedure loses exceptions that is very dangerous. Use writeFileAndReportErrors(java.io.File, java.lang.String) instead.

TODO rename it to writeFileWithNoExceptions Write text to file and report any errors via LOG.error()

Parameters:
file - file to write
text - text to write to file

writeFileAndReportErrors

public static void writeFileAndReportErrors(@NotNull
                                            java.io.File file,
                                            @NotNull
                                            java.lang.String text)
                                     throws java.io.IOException
Writes text to file. Throws IO exception if error occurred.

Parameters:
file - file to write text to
text - text to write in file
Throws:
java.io.IOException - if error occurred
Since:
6.5

writeFile

public static void writeFile(@NotNull
                             java.io.File file,
                             @NotNull
                             java.lang.String text,
                             @NotNull
                             java.lang.String encoding)
                      throws java.io.IOException
Writes text to file. Throws IO exception if error occurred.

Parameters:
file - file to write text to
text - text to write in file
encoding - encoding
Throws:
java.io.IOException - if error occurred
Since:
7.1

readXmlFile

public static void readXmlFile(@NotNull
                               java.io.File file,
                               @NotNull
                               FileUtil.Processor p)

parseDocument

public static org.jdom.Element parseDocument(@NotNull
                                             java.io.File file)
                                      throws org.jdom.JDOMException,
                                             java.io.IOException
Throws:
org.jdom.JDOMException
java.io.IOException

parseDocument

public static org.jdom.Element parseDocument(@NotNull
                                             java.io.File file,
                                             boolean validate)
                                      throws org.jdom.JDOMException,
                                             java.io.IOException
Throws:
org.jdom.JDOMException
java.io.IOException

parseDocument

public static org.jdom.Element parseDocument(@NotNull
                                             java.io.InputStream input,
                                             boolean validate)
                                      throws org.jdom.JDOMException,
                                             java.io.IOException
Throws:
org.jdom.JDOMException
java.io.IOException

parseDocument

public static org.jdom.Element parseDocument(@NotNull
                                             java.io.Reader input,
                                             boolean validate)
                                      throws org.jdom.JDOMException,
                                             java.io.IOException
Throws:
org.jdom.JDOMException
java.io.IOException
Since:
8.0

processXmlFile

public static void processXmlFile(@NotNull
                                  java.io.File file,
                                  @NotNull
                                  FileUtil.Processor p)

saveDocument

public static void saveDocument(@NotNull
                                org.jdom.Document document,
                                @NotNull
                                java.io.File file)
                         throws java.io.IOException
Throws:
java.io.IOException

getFilesSignature

@NotNull
public static java.lang.String getFilesSignature(@NotNull
                                                         java.util.Collection<java.io.File> _files)

createTempDirectory

@NotNull
public static java.io.File createTempDirectory(java.lang.String prefix,
                                                       java.lang.String suffix,
                                                       java.io.File in)
                                        throws java.io.IOException
Throws:
java.io.IOException

getFileInSameDir

@NotNull
public static java.io.File getFileInSameDir(@NotNull
                                                    java.io.File origFile,
                                                    java.lang.String neededFileName)
Returns a java.io.File object representing the file located in the same directory as origFile with the given file name

assert getFileInSameDir(new File("somDir/someFile.txt"), "anotherFile.txt"). equals(new File("someDir/anotherFile.txt"));

Parameters:
origFile - original file
neededFileName - name of required file
Returns:
required file

renameAndReportErrors

public static void renameAndReportErrors(@NotNull
                                         java.io.File oldName,
                                         @NotNull
                                         java.io.File newName)
                                  throws java.io.IOException,
                                         java.lang.SecurityException
Renames or moves the give file or directory.

Performs File.renameTo(java.io.File) but checks the result and throws IOException when the rename is unsuccessful.

Parameters:
oldName - old path and name.
newName - new path and name.
Throws:
java.io.IOException - when rename is unsuccessful.
java.lang.SecurityException - if a security manager exists and it has denied the operation.
Since:
8.0

sameButDifferInCase

public static boolean sameButDifferInCase(@NotNull
                                          java.io.File file1,
                                          @NotNull
                                          java.io.File file2)
Check whether the two given paths denotes the same file but in differ cases.

Note: the result is depend on the actual file system. In unix this function always returns false.

Parameters:
file1 - the first file to compare.
file2 - the second file to compare.
Returns:
differ but in case only.
Since:
8.0

renameFileNameOnly

@NotNull
public static java.io.File renameFileNameOnly(@NotNull
                                                      java.io.File file,
                                                      @NotNull
                                                      java.lang.String newName)
                                       throws java.io.IOException,
                                              java.lang.SecurityException
Throws:
java.io.IOException
java.lang.SecurityException

resolvePath

@NotNull
public static java.io.File resolvePath(@NotNull
                                               java.io.File baseDir,
                                               @NotNull
                                               java.lang.String path)
If path is absolute, returns path, otherwise it returns path within specified base directory.

Parameters:
baseDir - base directory for the resolving the path
path - path to be resolved, may be absolute or relative for baseDir
Returns:
absolute path File

copyResource

public static void copyResource(@NotNull
                                java.lang.String resourceName,
                                @NotNull
                                java.io.File outputFile)
Deprecated. Use copyResource(Class, String, java.io.File) to overcome classloader change issues

Copies contents of the specified resource into the specified file.

Parameters:
resourceName - Name of the classpath resource
outputFile - target file for the resource data

copyResource

public static void copyResource(@NotNull
                                java.lang.Class<?> clazz,
                                @NotNull
                                java.lang.String resourceName,
                                @NotNull
                                java.io.File outputFile)
Copies contents of the specified resource into the specified file. Uses Class.getResourceAsStream(String) in 'clazz' parameter to fetch the resource. NOTE: File will be created even if there was no such resource

Parameters:
clazz - class from the class loader that can access the resource
resourceName - Name of the classpath resource
outputFile - target file for the resource data

copyResourceIfNotExists

public static void copyResourceIfNotExists(@NotNull
                                           java.lang.Class<?> clazz,
                                           @NotNull
                                           java.lang.String resourceName,
                                           @NotNull
                                           java.io.File outputFile)
Copies the resource file into specified file if it doesn't exist (see copyResource(Class, String, java.io.File)).

Parameters:
clazz - class from the class loader that can access the resource
resourceName - the name of the classpath resource
outputFile - destination file for the resource data

copyResourceWithDist

public static void copyResourceWithDist(@NotNull
                                        java.lang.Class<?> clazz,
                                        @NotNull
                                        java.lang.String resourceName,
                                        @NotNull
                                        java.io.File outputFile)
Copies the resource file into specified file (see copyResource(Class, String, java.io.File)).

Handles the '.dist' file as well, i.e.:

Parameters:
clazz - class from the class loader that can access the resource
resourceName - the name of the classpath resource
outputFile - destination file for the resource data

readResourceAsString

@Nullable
public static java.lang.String readResourceAsString(@NotNull
                                                             java.lang.Class<?> clazz,
                                                             @NotNull
                                                             java.lang.String resourceName,
                                                             @NotNull
                                                             java.nio.charset.Charset charset)
                                             throws java.io.IOException
Reads the jar resource as a text.

Parameters:
clazz - class (used to determine the resource path).
resourceName - name of resource to read.
charset - character set of the resource file.
Returns:
text from resource, or null if the resource not found.
Throws:
java.io.IOException - when IO error occurs
Since:
6.0

copyStreams

public static void copyStreams(java.io.InputStream is,
                               java.io.OutputStream os)
                        throws java.io.IOException
Throws:
java.io.IOException

copyFilePart

public static void copyFilePart(@NotNull
                                java.io.File sourceFile,
                                long offset,
                                long length,
                                @NotNull
                                java.io.OutputStream destination)
                         throws java.io.IOException
Copies a part of a file to output stream. Stream is closed after part was copied.

Parameters:
sourceFile - file to copy
offset - file offset
length - length of data
destination - stream to write to
Throws:
java.io.IOException

copyStreamRangeAndClose

public static void copyStreamRangeAndClose(@NotNull
                                           java.io.InputStream source,
                                           long offset,
                                           long length,
                                           @NotNull
                                           java.io.OutputStream destination)
                                    throws java.io.IOException
Copies a stream range of a file to output stream. Stream would be closed at end

Parameters:
source - file to copy
offset - file offset
length - length of data
destination - stream to write to
Throws:
java.io.IOException
Since:
6.5
See Also:
TCStreamUtil.skip(java.io.InputStream, long), TCStreamUtil.writeBinary(java.io.InputStream, long, java.io.OutputStream)

close

public static void close(@Nullable
                         java.io.Closeable e)
Closes a resource if it is not null All possible IOExceptions are ignored

Parameters:
e - resource to close

close

public static void close(@Nullable
                         java.util.zip.ZipFile e)
Closes a resource if it is not null All possible IOExceptions are ignored

Parameters:
e - resource to close
Since:
8.0

closeAll

public static void closeAll(@NotNull
                            java.io.Closeable... toClose)
Closes all not-null resources All possible IOExceptions are ignored

Parameters:
toClose - resources to close
Since:
7.0

getSubDirectories

@NotNull
public static java.util.List<java.io.File> getSubDirectories(@NotNull
                                                                     java.io.File root)

createDir

@NotNull
public static java.io.File createDir(@NotNull
                                             java.io.File dir)
                              throws java.io.IOException
Creates directory if it does not exist. Throws IOException if directory creation was not successful.

Parameters:
dir - directory to be created
Returns:
dir
Throws:
java.io.IOException - if failed to create directory

createEmptyDir

public static java.io.File createEmptyDir(@NotNull
                                          java.io.File dir)
                                   throws java.io.IOException
Creates an empty directory, removing all content if it already exists.

Parameters:
dir - the directory to create
Returns:
result file
Throws:
java.io.IOException - in case of I/O error occurred

getTotalDirectorySize

public static long getTotalDirectorySize(@Nullable
                                         java.io.File dir)
Returns the directory size in bytes of files under the specified directory (non recursive).

Parameters:
dir - the directory to traverse
Returns:
size in bytes

getTotalDirectorySize

public static long getTotalDirectorySize(@Nullable
                                         java.io.File dir,
                                         boolean recursively)
Returns the directory size in bytes of files under the specified directory recursively.

Parameters:
dir - the directory to traverse
Returns:
size in bytes
Since:
7.1.4

deleteIfEmpty

public static void deleteIfEmpty(@NotNull
                                 java.io.File dir)
Removes directory if it is empty

Parameters:
dir - directory to be removed

delete

public static java.util.Set<java.io.File> delete(@NotNull
                                                 java.io.File fileOrDirectory,
                                                 int numAttempts)
Removes specified directory or file, for each removed file performs operation up to numAttempts. If directory is provided, all files under this directory will be removed recursively.

Parameters:
fileOrDirectory - file or directory to remove
numAttempts - maximum number of attempts to perform (must be positive)
Returns:
set of successfully removed files
Since:
8.0

delete

public static java.util.Set<java.io.File> delete(@NotNull
                                                 java.io.File fileOrDirectory,
                                                 @NotNull
                                                 java.io.FileFilter fileFilter,
                                                 int numAttempts)
Removes specified directory or file, for each removed file performs operation up to numAttempts

Parameters:
fileOrDirectory - file or directory to remove
numAttempts - maximum number of attempts to perform (must be positive)
fileFilter - filter for files, must return true for files to be removed and false otherwise
Returns:
set of successfully removed files
Since:
8.0

delete

public static boolean delete(@NotNull
                             java.io.File file)
Removes specified file or directory, same as FileUtil.delete(file, 10).

Parameters:
file - file or directory to remove
Returns:
true if file or directory was removed and false otherwise

isEmptyDir

public static boolean isEmptyDir(@NotNull
                                 java.io.File dir)
Returns true if directory is empty or does not exist and false otherwise.

Parameters:
dir - directory
Returns:
true if dir is empty or does not exist

isReadonly

public static boolean isReadonly(@NotNull
                                 java.io.File file)
Returns true if file is readonly and false otherwise

Parameters:
file - file
Returns:
true if file is readonly

fixDirectoryName

@NotNull
public static java.lang.String fixDirectoryName(@NotNull
                                                        java.lang.String name)
Fixes directory name: replaces all dangerous characters with '_'.

Parameters:
name - name of the directory to fix
Returns:
name with all 'dangerous' characters replaced with '_'

isBadDirectoryName

public static boolean isBadDirectoryName(@NotNull
                                         java.lang.String name)
Checks whether specified name contains dangerous characters which might prevent successful directory creation.

Parameters:
name - name to check (just name not path)
Returns:
true if directory with given name cannot be created because of invalid characters in it

normalizeSeparator

@NotNull
public static java.lang.String normalizeSeparator(@NotNull
                                                          java.lang.String filePath)
Replace all separators like / and \ to File.separatorChar

Parameters:
filePath - path to ve normalized
Returns:
path with replaced separators

normalizeRelativePath

@NotNull
public static java.lang.String normalizeRelativePath(@NotNull
                                                             java.lang.String path)
Normalizes relative path by eliminating all possible path/../ /./ ./path combinations

Parameters:
path - relative path
Returns:
normalized paths with all slashes replaced with '/' and all dot combinations removed (if possible)
Since:
4.0.2

sortFiles

public static void sortFiles(@NotNull
                             java.io.File[] files)

getCommonParent

@NotNull
public static java.lang.String getCommonParent(@NotNull
                                                       java.lang.String firstUrl,
                                                       @NotNull
                                                       java.lang.String secondUrl)

isFileSeparator

public static boolean isFileSeparator(char c1)

getCanonicalFile

@NotNull
public static java.io.File getCanonicalFile(@NotNull
                                                    java.io.File file)
Attempts to obtain canonical file from the specified file and if it fails returns absolute file.

Parameters:
file - file
Returns:
see above

checkTmpDir

public static void checkTmpDir()
Checks if it is allowed to use tmpDir taken from "java.io.tmpdir" system property

Throws:
java.lang.IllegalStateException - if there is no access to temp dir.

findFile

@Nullable
public static java.io.File findFile(@NotNull
                                             java.io.FileFilter filter,
                                             @NotNull
                                             java.io.File... directories)
Searches for a file in the specified directories recursively.

Parameters:
filter - filter which checks whether file is acceptable
directories - directories to search for (recursively)
Returns:
first found file or null

findFiles

@NotNull
public static java.util.Collection<java.io.File> findFiles(@NotNull
                                                                   java.io.FileFilter filter,
                                                                   @NotNull
                                                                   java.io.File... directories)
Searches for files in the specified directories recursively.

Parameters:
filter - filter which checks whether file is acceptable
directories - directories to search for (recursively)
Returns:
all found files

listFilesRecursively

public static <C extends java.util.Collection<java.lang.String>> C listFilesRecursively(@NotNull
                                                                                        java.io.File dir,
                                                                                        @NotNull
                                                                                        java.lang.String prefix,
                                                                                        boolean includeDirs,
                                                                                        int depthLimit,
                                                                                        @Nullable
                                                                                        Predicate<java.io.File> predicate,
                                                                                        @NotNull
                                                                                        C collection)
Traverses the given directory and retrieves all files (and nested directories, if specified). Puts the found files and directories into the given collection as strings, where each string is a relative path from the given directory, with '/' path separator (independent on the current OS).

If the includeDirs option is false, only files are listed. Empty directories are skipped. If this option is true, all directories are included (empty and non-empty) as separated entries.

Type Parameters:
C - type of the destination collection.
Parameters:
dir - directory to travers.
prefix - prefix that will be able to each entry.
includeDirs - include directories as separated entries.
depthLimit - recursion depth; 0 means non-recursive; Integer.MAX_VALUE means infinite depth.
predicate - a predicate that is applied to each file or directory; if not met - file or directory will not be included and the directory will not be traversed in.
collection - destination collection where to put found file entries (must be writable).
Returns:
the given destination collection.
Since:
7.1

listFiles

@NotNull
public static java.io.File[] listFiles(@NotNull
                                               java.io.File file,
                                               @NotNull
                                               java.io.FilenameFilter filter)
A simple wrapper around file.listFiles(filter) that handles possible null result. In this case an empty array is returned.

Parameters:
file - the file
filter - the filter to apply
Returns:
the array of inner file names (possibly empty)

getFreeSpace

@Nullable
public static java.lang.Long getFreeSpace(@NotNull
                                                   java.io.File file)
Computes free space on the disk that contains a file

Parameters:
file - file to select disk
Returns:
free space in bytes or null is operation is not supported by current JVM
Since:
5.0

copy

public static void copy(@NotNull
                        java.io.File from,
                        @NotNull
                        java.io.File to)
                 throws java.io.IOException
Throws:
java.io.IOException

areTheSame

public static boolean areTheSame(@Nullable
                                 java.io.File file1,
                                 @Nullable
                                 java.io.File file2)
                          throws java.io.IOException
Checks that both file pointers specify the same object. So, in Linux it should denote symbolic links. In Windows it should take into account that names are case insensitive.

Note: it also returns true if both pointers are null.

Parameters:
file1 - the first file pointer.
file2 - the second file pointer.
Returns:
sameness.
Throws:
java.io.IOException

convertAntToRegexp

@NotNull
public static java.lang.String convertAntToRegexp(@NotNull
                                                          java.lang.String antPattern)

convertAntToRegexp

@NotNull
public static java.lang.String convertAntToRegexp(@NotNull
                                                          java.lang.String antPattern,
                                                          boolean ignoreStartingSlash)
Parameters:
antPattern - ant-style path pattern
Returns:
java regexp pattern. Note that no matter whether forward or backward slashes were used in the antPattern the returned regexp pattern will use forward slashes ('/') as file separators. Paths containing windows-style backslashes must be converted before matching against the resulting regexp
See Also:
FileUtil.toSystemIndependentName(java.lang.String)

getExtension

@NotNull
public static java.lang.String getExtension(@NotNull
                                                    java.lang.String fileNameWithDirectory)
Unlike com.intellij.openapi.util.io.FileUtil.getExtension, can use this with full full file name (including directory name).

Parameters:
fileNameWithDirectory -
Returns:

getNameWithoutExtension

@NotNull
public static java.lang.String getNameWithoutExtension(@NotNull
                                                               java.io.File file)

getNameWithoutExtension

@NotNull
public static java.lang.String getNameWithoutExtension(@NotNull
                                                               java.lang.String name)

setExectuableAttribute

public static void setExectuableAttribute(java.lang.String path,
                                          boolean executableFlag)
                                   throws java.io.IOException
Set executable attribute, it makes sense only on non-windows platforms.

Parameters:
path - the path to use
executableFlag - new value of executable attribute
Throws:
java.io.IOException - if there is a problem with setting the flag

checkContentEqual

public static boolean checkContentEqual(@NotNull
                                        java.io.File file1,
                                        @NotNull
                                        java.io.File file2)
Check both files exist and are same byte-by-byte.

Parameters:
file1 - first file
file2 - second file
Returns:
true if both files exist and have exactly same content

isAncestor

public static boolean isAncestor(@NotNull
                                 java.io.File ancestor,
                                 @NotNull
                                 java.io.File file,
                                 boolean strict)
Check if the ancestor is an ancestor of file. Overrides the same method from FileUtil. Please do not use the method from super class directly - it can cause the compilation problems.

Parameters:
ancestor - the file
file - the file
strict - if false then this method returns true if ancestor and file are equal
Returns:
true if ancestor is parent of file; false otherwise

copy

public static void copy(@NotNull
                        java.io.InputStream is,
                        @NotNull
                        java.io.OutputStream os)
                 throws java.io.IOException
Same as copyStream.

Throws:
java.io.IOException

isBinaryFile

public static boolean isBinaryFile(@NotNull
                                   java.io.File file)
                            throws java.io.IOException
Returns whether a specified file is a binary file.

Parameters:
file - the file to analyse
Returns:
true iff file is binary
Throws:
java.io.IOException - in case of I/O errors

isBinaryData

public static boolean isBinaryData(byte[] data,
                                   int size)
Returns whether specified byte array represents binary data (not text)

Parameters:
data - the byte array
size - size to check
Returns:
true iff data array represents binary data

isTextData

public static boolean isTextData(byte[] data,
                                 int size)
Returns whether specified byte array represents text data (not binary)

Parameters:
data - the byte array
size - size to check
Returns:
true iff data array represents text data