Class IOHelper
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcleanupFileName(String p_strFileName) Delete all characters from the given file or directory name that are not supported by the current operating system.static voidClears a directory recursively but doesn't delete it.static booleanclearDirectory(File p_dir) Recursively clear the given directory.static longconsumeAll(InputStream p_in) Read and discard all bytes from the given input stream.static voidcopyChannel(FileChannel p_dest, FileChannel p_src, long p_lUnusedParameter) static voidCopy a directory recursively.static voidCopy a directory recursively.static voidcopyDir(File p_dest, File p_src, FileFilter p_filter) Copy a directory recursively.static voidcopyDir(File p_dirDst, File p_dirSrc, FileFilter p_filter, boolean p_bCopyMarkedDirs) Copy a directory recursively.static voidPerforms buffered copy from one file to another.static voidcopyFileOrDirectory(File p_src, File p_dest, String p_strItem) Copy a directory from a src dir to a dest dir recursively.static voidcopyStream(OutputStream p_dest, InputStream p_src) Performs buffered copy from one stream to another.static FilecreateTempDirectory(File p_dirParent, String p_strPrefix, String p_strSuffix) Create a new directory with a unique name.static booleandeleteFile(File p_file) Same asdeleteFileRecursively(File).static booleandeleteFile(String p_strFile) Delete files recursivelystatic booleandeleteFileRecursively(File p_file) Delete files recursively.static booleandeleteFileRecursively(Path p_path) Delete files recursively.static booleanexistsFileIgnoreCase(Path p_dir, String p_strFileName) Case insensitively check if in the given directory exists a file or directory with the given name.static StringgetBase64StringFromFile(String p_strFile) static longComputes the CRC-32 of the given file.static longgetCRC32(InputStream p_is) Computes the CRC-32 of the given stream.static FileGet a file object that represents a file or directory in a sub-path below a given directory.static FilegetFileBelow(File p_dir, String... p_path) Get a file object that represents a file or directory in a sub-path below a given directory.static FilegetFileBelow(String p_strPath) Get a file object that represents a file or directory in a sub-path below the Java VM's working directory.static byte[]getFileBytes(File p_file) Deprecated.static byte[]getFileBytes(Path p_file) Deprecated.UseFiles.readAllBytes(Path)instead.static intgetFileNumber(File p_file) static longgetFileSize(File p_file) static StringgetFileText(File p_file, String p_strEncoding) Get the content of a small text file.static StringgetFileText(File p_file, Charset p_encoding) Get the content of a small text file.static StringgetFileText(Path p_file, String p_strEncoding) Deprecated.UseNIOHelper.getFileText(Path)orNIOHelper.getFileText(Path, Charset)instead.static StringgetFileText(Path p_file, Charset p_encoding) Deprecated.UseNIOHelper.getFileText(Path, Charset)instead.static StringgetNormalizedPath(File p_file) Get the normalized path of an existing file or directory.static StringgetPathRelative(File p_dir, File p_file) Get the path of an existing file or directory relative to a given existing directory.static FilegetUniqueFileName(File p_dirDest, String p_strFilename) Create a file object with a non existing file name on the destination directory.static FilegetUniqueNumberedFile(File p_file) static booleanChecks if the specified file is a child of the specified directory or of its child directories.static booleanisDirectoryEmpty(File p_dir) Check if the given directory is empty or not.static booleanisDirectoryEmpty(Path p_dir) Check if the given directory is empty or not.static booleanisFileBelow(File p_dir, File p_file) Check if the given file's location is below the given directory.static booleanisSameFile(File p_file1, File p_file2) Deprecated.UseFiles.isSameFile(Path, Path)instead.static voidMove a file from one place to another.static voidsetFileOwner(String p_strPath, String p_strOwner, boolean p_bRecursive) Deprecated.Since 7.0 use version with group name insteadstatic voidsetFileOwner(String p_strPath, String p_strOwner, String p_strGroup, boolean p_bRecursive) Set the owner of a file or directory.
Wildcard characters do not work!!!static voidsetFileOwner(String p_strPath, String p_strOwner, String p_strGroup, boolean p_bRecursive, boolean p_bUseSudo) Set the owner of a file or directory.
Wildcard characters do not work!!!static voidsetFilePermissions(String p_strPath, int p_iUser, int p_iGroup, int p_iOthers, boolean p_bRecursive) Set access permissions of a file or directory.
Wildcard characters do not work!!!static StringstringFromBase64ByteArray(String p_strBase64, String p_strCharset) static FilestripFileNumber(File p_file) static voidtarDir(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream p_tar, String p_strRootDir, File p_dirSrc, boolean p_bTarMarkedDirs) Deprecated.static voidDeprecated.UseTarHelper.untarDir(File, String, String, File)instead.static voidDeprecated.UseZipHelper.unzipDir(File, File)instead.static voidDeprecated.UseZipHelper.unzipDir(ZipFile, String, File)instead.static voidunzipDir(ZipInputStream p_zip, String p_strPrefixFilter, File p_dirDst) Deprecated.UseZipHelper.unzipDir(ZipInputStream, String, File)instead.static voidwriteFileBytes(File p_file, byte[] p_bytes) Write the content of a small file.static voidwriteFileBytes(Path p_file, byte[] p_bytes) Write the content of a small file.static voidwriteFileText(File p_file, String p_strText, String p_strEncoding) Write the content of a small text file.static voidwriteFileText(File p_file, String p_strText, Charset p_encoding) Write the content of a small text file.static voidwriteFileText(Path p_file, String p_strText, String p_strEncoding) Write the content of a small text file.static voidwriteFileText(Path p_file, String p_strText, Charset p_encoding) Write the content of a small text file.static voidDeprecated.UseZipHelper.zipDir(File, File)instead.static voidzipDir(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc) Deprecated.UseZipHelper.zipDir(ZipOutputStream, String, File)instead.static voidzipDir(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc, boolean p_bZipMarkedDirs) Deprecated.
-
Field Details
-
READ
public static final int READconstant for read access right- See Also:
-
WRITE
public static final int WRITEconstant for write access right- See Also:
-
EXECUTE
public static final int EXECUTEconstant for execution right- See Also:
-
-
Constructor Details
-
IOHelper
public IOHelper()
-
-
Method Details
-
existsFileIgnoreCase
Case insensitively check if in the given directory exists a file or directory with the given name.The search is not recursive over directories.
If
p_diris a symbolic link the link is followed.Note: This operation may be expensive if the directory contains many files.
- Parameters:
p_dir- The directory to be searched.p_strFileName- The name of the file to be searched for.- Returns:
trueif the file exists, orfalseotherwise.- Throws:
IOException- if one occurs
-
copyStream
Performs buffered copy from one stream to another.
The destination buffer is being flushed when copying is ready.
- Parameters:
p_dest- The destination stream.p_src- The source stream.- Throws:
IOException- If an I/O-error occurs.
-
copyChannel
public static void copyChannel(FileChannel p_dest, FileChannel p_src, long p_lUnusedParameter) throws IOException - Throws:
IOException
-
isDirectoryEmpty
Check if the given directory is empty or not.- Parameters:
p_dir- The directory.- Returns:
trueif the given directory is empty, orfalseotherwise.- Throws:
IOException- If an I/O error occurred.
-
isDirectoryEmpty
Check if the given directory is empty or not.- Parameters:
p_dir- The directory.- Returns:
trueif the given directory is empty, orfalseotherwise.- Throws:
IOException- If an I/O error occurred.
-
getFile
Get a file object that represents a file or directory in a sub-path below a given directory.Note: The file object is constructed without any checks if the path references an existing file system object or if it is below the given directory. For a security enhanced version see
getFileBelow(File, String...).- Parameters:
p_dir- The start directory.p_path- The sub-path.- Returns:
- A file object that represents a file or directory in a sub-path below the given directory.
-
getFileBelow
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 given directory.- Returns:
- A file object that represents a file or directory in a sub-path of the given directory.
- Throws:
IllegalArgumentException- Ifp_dirisnull, or ifp_strPathisnullor empty.SecurityException- Ifp_strPathreferences a file location outside the given directory.
-
isFileBelow
Check if the given file's location is below the given directory.- Parameters:
p_dir- The containing directory.p_file- The sub-path within the given directory.- Returns:
trueif the given file's location is below the given, directory, orfalseotherwise.- Throws:
IOException- If an I/O error occurred.IllegalArgumentException- Ifp_dirisnull, or ifp_strPathisnullor empty.
-
getFileBelow
Get a file object that represents a file or directory in a sub-path below a given directory.- Parameters:
p_dir- The containing directory.p_path- 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- Ifp_dirisnull, or if the directoryp_dirdoes not exist, or ifp_pathisnullor contains empty path elements.SecurityException- Ifp_pathreferences a file location outside the given directory.
-
moveFile
Move a file from one place to another.- Parameters:
p_fileDst- The destination file (must not already exist).p_fileSrc- The source file.- Throws:
FileNotFoundException- If the source file does not exist.IOException- If an I/O-error occurs, or if the destination file already exists.
-
copyFile
Performs buffered copy from one file to another.Java NIO and Path use:
seeFiles#copy(InputStream, Path, java.nio.file.CopyOption...)
Example:Files.copy(p_fileSrc, p_fileDst, StandardCopyOption.REPLACE_EXISTING)- Parameters:
p_fileDst- The destination file.p_fileSrc- The source file.- Throws:
IOException- If an I/O-error occurs.
-
copyDir
Copy a directory recursively.p_destis the parent directory of the new directory.- Parameters:
p_dest- The destination directory.p_src- The directory to copy.- Throws:
IOException- If an I/O-error occurs.
-
copyDir
Copy a directory recursively.p_destis the parent directory of the new directory.- Parameters:
p_dest- destination directoryp_src- directory to copyp_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.
-
copyDir
Copy a directory recursively. Copy only the files matching the specified file filter.
p_destis the parent directory of the new directory.- Parameters:
p_dest- destination directoryp_src- directory to copyp_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 occurs.
-
copyDir
public static void copyDir(File p_dirDst, File p_dirSrc, FileFilter p_filter, boolean p_bCopyMarkedDirs) throws IOException Copy a directory recursively. Copy only the files matching the specified file filter.
p_destis the parent directory of the new directory.- Parameters:
p_dirDst- The destination directory.p_dirSrc- The directory to copy.p_filter- File filter that filters the files that will be copied. Ifnullis 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 occurs.
-
clearDir
Clears a directory recursively but doesn't delete it.- Parameters:
p_dir- The directory to clear.
-
copyFileOrDirectory
public static void copyFileOrDirectory(File p_src, File p_dest, String p_strItem) throws IOException Copy a directory from a src dir to a dest dir recursively.- Parameters:
p_src- The source directory of the files.p_dest- The destination path for the files.p_strItem- File or directory to copy, relative top_src.- Throws:
IOException
-
setFilePermissions
public static void setFilePermissions(String p_strPath, int p_iUser, int p_iGroup, int p_iOthers, boolean p_bRecursive) throws IOException Set access permissions of a file or directory.
Wildcard characters do not work!!!- Parameters:
p_strPath- path to a file or directoryp_iUser- permissions for the user who owns the filesp_iGroup- permissions for the groupp_iOthers- permissions for all other usersp_bRecursive- change permissions recursively for directories- Throws:
IOException- error during execution of the external permissions command
-
setFileOwner
@Deprecated public static void setFileOwner(String p_strPath, String p_strOwner, boolean p_bRecursive) throws IOException Deprecated.Since 7.0 use version with group name insteadSet the owner of a file or directory.
Wildcard characters do not work!!!
The owner group will not be changed.- Parameters:
p_strPath- path to a file or directoryp_strOwner- owner userp_bRecursive- change permissions recursively for directories- Throws:
IOException- error during execution of the external permissions command
-
setFileOwner
public static void setFileOwner(String p_strPath, String p_strOwner, String p_strGroup, boolean p_bRecursive) throws IOException Set the owner of a file or directory.
Wildcard characters do not work!!!- Parameters:
p_strPath- path to a file or directoryp_strOwner- owner userp_strGroup- owner group to set, null if nonep_bRecursive- change permissions recursively for directories- Throws:
IOException- error during execution of the external permissions command
-
setFileOwner
public static void setFileOwner(String p_strPath, String p_strOwner, String p_strGroup, boolean p_bRecursive, boolean p_bUseSudo) throws IOException Set the owner of a file or directory.
Wildcard characters do not work!!!- Parameters:
p_strPath- path to a file or directoryp_strOwner- owner userp_strGroup- owner group to set, null if nonep_bRecursive- change permissions recursively for directoriesp_bUseSudo- run chown command as sudo- Throws:
IOException- error during execution of the external permissions command
-
deleteFile
Same asdeleteFileRecursively(File).Note: do not use this method in scripts.
- See Also:
-
deleteFileRecursively
Delete files recursively.- Returns:
trueif successful, orfalseif the file does not exist or any subdirectory could not be deleted.
-
deleteFileRecursively
Delete files recursively.- Returns:
trueif successful, orfalseif the file does not exist or any subdirectory could not be deleted.
-
clearDirectory
Recursively clear the given directory.The directory itself is not being deleted.
- Returns:
trueif successful,falseif the directory does not exist or any sub directory could not be deleted.
-
deleteFile
Delete files recursively- Returns:
- true if successful
-
getNormalizedPath
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.
If
p_filedenotes a directory the returned path has a trailing forward slash.- Parameters:
p_file- The file.- Returns:
- The normalized path.
- Throws:
IOException- - If an I/O error occurred.
-
getPathRelative
Get the path of an existing file or directory relative to a given existing 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_filedenotes a directory the returned path, if notnull, has a trailing forward slash.- Parameters:
p_dir- The base directory.p_file- The file for which the path relative top_dirshould be determined.- Returns:
- The path of
p_filerelative top_dir, ornullifp_diris not a parent ofp_file. - Throws:
IOException- - If an I/O error occurred.
-
stripFileNumber
- Returns:
- the file with removed trailing file number
-
getFileNumber
- Returns:
- the number at the end of the file name
-
getUniqueNumberedFile
- Returns:
- A file path base on the given file that does not exist.
-
getBase64StringFromFile
- Returns:
- the contents of the given file in a string with base64 encoding
- Throws:
IOException
-
stringFromBase64ByteArray
public static String stringFromBase64ByteArray(String p_strBase64, String p_strCharset) throws IOException - Returns:
- the string content of a base64 encoded byte array using the given character set
- Throws:
IOException
-
consumeAll
Read and discard all bytes from the given input stream.- Parameters:
p_in- The input stream.- Returns:
- The number of bytes read.
- Throws:
IOException
-
getFileBytes
Deprecated.UseFiles.readAllBytes(Path)instead.- Returns:
- The file contents as a byte array.
- Throws:
IOException
-
getFileBytes
Deprecated.UseFiles.readAllBytes(Path)instead.- Returns:
- The file contents as a byte array.
- Throws:
IOException
-
writeFileBytes
Write the content of a small file.- Parameters:
p_file- The file.p_bytes- The bytes to be written.- Throws:
IOException- If an I/O error occurred.
-
writeFileBytes
Write the content of a small file.- Parameters:
p_file- The file.p_bytes- The bytes to be written.- Throws:
IOException- If an I/O error occurred.
-
getFileText
Get the content of a small text file.Note: Do not load large files as strings!
- Parameters:
p_file- The text file.p_strEncoding- The file encoding.- Returns:
- The content of the given text file, or an empty string if the
given file has size
0. - Throws:
FileNotFoundException- If the file does not exist.IOException- If an I/O error occurred, or the given file is too big.
-
getFileText
Get the content of a small text file.Note: Do not load large files as strings!
- Parameters:
p_file- The text file.p_encoding- The file encoding.- Returns:
- The content of the given text file, or an empty string if the
given file has size
0. - Throws:
FileNotFoundException- If the file does not exist.IOException- If an I/O error occurred, or the given file is too big.
-
getFileText
Deprecated.UseNIOHelper.getFileText(Path)orNIOHelper.getFileText(Path, Charset)instead.- Throws:
IOException
-
getFileText
Deprecated.UseNIOHelper.getFileText(Path, Charset)instead.- Throws:
IOException
-
writeFileText
public static void writeFileText(File p_file, String p_strText, String p_strEncoding) throws IOException Write the content of a small text file.Note: Do not write large files as strings!
- Parameters:
p_file- The text file.p_strText- The new text file content.p_strEncoding- The file encoding.- Throws:
IOException- If an I/O error occurred.
-
writeFileText
public static void writeFileText(File p_file, String p_strText, Charset p_encoding) throws IOException Write the content of a small text file.Note: Do not write large files as strings!
- Parameters:
p_file- The text file.p_strText- The new text file content.p_encoding- The file encoding.- Throws:
IOException- If an I/O error occurred.
-
writeFileText
public static void writeFileText(Path p_file, String p_strText, String p_strEncoding) throws IOException Write the content of a small text file.Note: Do not write large files as strings!
- Parameters:
p_file- The text file.p_strText- The new text file content.p_strEncoding- The file encoding.- Throws:
IOException- If an I/O error occurred.
-
writeFileText
public static void writeFileText(Path p_file, String p_strText, Charset p_encoding) throws IOException Write the content of a small text file.Note: Do not write large files as strings!
- Parameters:
p_file- The text file.p_strText- The new text file content.p_encoding- The file encoding.- Throws:
IOException- If an I/O error occurred.
-
zipDir
Deprecated.UseZipHelper.zipDir(File, File)instead.- Throws:
IOException
-
zipDir
@Deprecated public static void zipDir(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc) throws IOException Deprecated.UseZipHelper.zipDir(ZipOutputStream, String, File)instead.- Throws:
IOException
-
zipDir
@Deprecated public static void zipDir(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc, boolean p_bZipMarkedDirs) throws IOException Deprecated.- Throws:
IOException
-
unzipDir
@Deprecated public static void unzipDir(ZipInputStream p_zip, String p_strPrefixFilter, File p_dirDst) Deprecated.UseZipHelper.unzipDir(ZipInputStream, String, File)instead. -
unzipDir
Deprecated.UseZipHelper.unzipDir(File, File)instead.- Throws:
IOException
-
unzipDir
@Deprecated public static void unzipDir(ZipFile p_zip, String p_strRootDir, File p_dirDst) throws IOException Deprecated.UseZipHelper.unzipDir(ZipFile, String, File)instead.- Throws:
IOException
-
tarDir
@Deprecated public static void tarDir(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream p_tar, String p_strRootDir, File p_dirSrc, boolean p_bTarMarkedDirs) throws IOException Deprecated.- Throws:
IOException
-
untarDir
@Deprecated public static void untarDir(File p_fileArchive, String p_strRootDir, File p_dirDst) throws IOException Deprecated.UseTarHelper.untarDir(File, String, String, File)instead.- Throws:
IOException
-
getCRC32
Computes the CRC-32 of the given file.- Throws:
IOException
-
getCRC32
Computes the CRC-32 of the given stream.- Throws:
IOException
-
isSameFile
Deprecated.UseFiles.isSameFile(Path, Path)instead.Checks if the two given file objects represent the same file in the file system.- Parameters:
p_file1- The first file.p_file2- The second file.- Returns:
trueif the two file objects represent the same file in the file system, orfalseotherwise.- Throws:
IOException- If an I/O error occurred.
-
isChild
Checks if the specified file is a child of the specified directory or of its child directories.- Parameters:
p_dir- The directory directory to check for.p_file- The file to check.- Throws:
IOException
-
getFileSize
-
createTempDirectory
public static File createTempDirectory(File p_dirParent, String p_strPrefix, String p_strSuffix) throws IOException Create a new directory with a unique name.The caller is responsible for deleting the directory and it's contents.
- Parameters:
p_dirParent- The directory where the new temp directory should be created.p_strPrefix- A name prefix to be used (may benull).p_strSuffix- A name suffix to be used (may benull).- Returns:
- A reference to the newly created directory.
- Throws:
IOException- - If an error occurred.
-
getUniqueFileName
Create a file object with a non existing file name on the destination directory.- Parameters:
p_dirDest- The destination directory.p_strFilename- The filename.- Returns:
- The file object with a unique file name.
- Throws:
IOException- - If an error occurred.
-
cleanupFileName
Delete all characters from the given file or directory name that are not supported by the current operating system.Note: Use with care
Usually cleaning up file names depends on the actual use case as well as on the environment. This cannot always be accomplished by a common method like this.
The caller is responsible to check if the result of this method call is empty or not.
- Parameters:
p_strFileName- The name (i.e. the last part of the path) of the file or directory.- Returns:
- The clean file name.
-
Files.readAllBytes(Path)instead.