Class IOHelper
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
cleanupFileName
(String p_strFileName) Delete all characters from the given file or directory name that are not supported by the current operating system.static void
Clears a directory recursively but doesn't delete it.static boolean
clearDirectory
(File p_dir) Recursively clear the given directory.static long
consumeAll
(InputStream p_in) Read and discard all bytes from the given input stream.static void
copyChannel
(FileChannel p_dest, FileChannel p_src, long p_lUnusedParameter) static void
Copy a directory recursively.static void
Copy a directory recursively.static void
copyDir
(File p_dest, File p_src, FileFilter p_filter) Copy a directory recursively.static void
copyDir
(File p_dirDst, File p_dirSrc, FileFilter p_filter, boolean p_bCopyMarkedDirs) Copy a directory recursively.static void
Performs buffered copy from one file to another.static void
copyFileOrDirectory
(File p_src, File p_dest, String p_strItem) Copy a directory from a src dir to a dest dir recursively.static void
copyStream
(OutputStream p_dest, InputStream p_src) Performs buffered copy from one stream to another.static File
createTempDirectory
(File p_dirParent, String p_strPrefix, String p_strSuffix) Create a new directory with a unique name.static boolean
deleteFile
(File p_file) Same asdeleteFileRecursively(File)
.static boolean
deleteFile
(String p_strFile) Delete files recursivelystatic boolean
deleteFileRecursively
(File p_file) Delete files recursively.static boolean
deleteFileRecursively
(Path p_path) Delete files recursively.static boolean
existsFileIgnoreCase
(Path p_dir, String p_strFileName) Case insensitively check if in the given directory exists a file or directory with the given name.static String
getBase64StringFromFile
(String p_strFile) static long
Computes the CRC-32 of the given file.static long
getCRC32
(InputStream p_is) Computes the CRC-32 of the given stream.static File
Get a file object that represents a file or directory in a sub-path below a given directory.static File
getFileBelow
(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 File
getFileBelow
(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 int
getFileNumber
(File p_file) static long
getFileSize
(File p_file) static String
getFileText
(File p_file, String p_strEncoding) Get the content of a small text file.static String
getFileText
(File p_file, Charset p_encoding) Get the content of a small text file.static String
getFileText
(Path p_file, String p_strEncoding) Deprecated.UseNIOHelper.getFileText(Path)
orNIOHelper.getFileText(Path, Charset)
instead.static String
getFileText
(Path p_file, Charset p_encoding) Deprecated.UseNIOHelper.getFileText(Path, Charset)
instead.static String
getNormalizedPath
(File p_file) Get the normalized path of an existing file or directory.static String
getPathRelative
(File p_dir, File p_file) Get the path of an existing file or directory relative to a given existing directory.static File
getUniqueFileName
(File p_dirDest, String p_strFilename) Create a file object with a non existing file name on the destination directory.static File
getUniqueNumberedFile
(File p_file) static boolean
Checks if the specified file is a child of the specified directory or of its child directories.static boolean
isDirectoryEmpty
(File p_dir) Check if the given directory is empty or not.static boolean
isDirectoryEmpty
(Path p_dir) Check if the given directory is empty or not.static boolean
isFileBelow
(File p_dir, File p_file) Check if the given file's location is below the given directory.static boolean
isSameFile
(File p_file1, File p_file2) Deprecated.UseFiles.isSameFile(Path, Path)
instead.static void
Move a file from one place to another.static void
setFileOwner
(String p_strPath, String p_strOwner, boolean p_bRecursive) Deprecated.Since 7.0 use version with group name insteadstatic void
setFileOwner
(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 void
setFileOwner
(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 void
setFilePermissions
(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 String
stringFromBase64ByteArray
(String p_strBase64, String p_strCharset) static File
stripFileNumber
(File p_file) static void
tarDir
(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream p_tar, String p_strRootDir, File p_dirSrc, boolean p_bTarMarkedDirs) Deprecated.static void
Deprecated.UseTarHelper.untarDir(File, String, String, File)
instead.static void
Deprecated.UseZipHelper.unzipDir(File, File)
instead.static void
Deprecated.UseZipHelper.unzipDir(ZipFile, String, File)
instead.static void
unzipDir
(ZipInputStream p_zip, String p_strPrefixFilter, File p_dirDst) Deprecated.UseZipHelper.unzipDir(ZipInputStream, String, File)
instead.static void
writeFileBytes
(File p_file, byte[] p_bytes) Write the content of a small file.static void
writeFileBytes
(Path p_file, byte[] p_bytes) Write the content of a small file.static void
writeFileText
(File p_file, String p_strText, String p_strEncoding) Write the content of a small text file.static void
writeFileText
(File p_file, String p_strText, Charset p_encoding) Write the content of a small text file.static void
writeFileText
(Path p_file, String p_strText, String p_strEncoding) Write the content of a small text file.static void
writeFileText
(Path p_file, String p_strText, Charset p_encoding) Write the content of a small text file.static void
Deprecated.UseZipHelper.zipDir(File, File)
instead.static void
zipDir
(ZipOutputStream p_zip, String p_strRootDir, File p_dirSrc) Deprecated.UseZipHelper.zipDir(ZipOutputStream, String, File)
instead.static void
zipDir
(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_dir
is 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:
true
if the file exists, orfalse
otherwise.- 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:
true
if the given directory is empty, orfalse
otherwise.- Throws:
IOException
- If an I/O error occurred.
-
isDirectoryEmpty
Check if the given directory is empty or not.- Parameters:
p_dir
- The directory.- Returns:
true
if the given directory is empty, orfalse
otherwise.- 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_dir
isnull
, or ifp_strPath
isnull
or empty.SecurityException
- Ifp_strPath
references 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:
true
if the given file's location is below the given, directory, orfalse
otherwise.- Throws:
IOException
- If an I/O error occurred.IllegalArgumentException
- Ifp_dir
isnull
, or ifp_strPath
isnull
or 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_dir
isnull
, or if the directoryp_dir
does not exist, or ifp_path
isnull
or contains empty path elements.SecurityException
- Ifp_path
references 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_dest
is 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_dest
is 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_dest
is 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_dest
is 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. Ifnull
is 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:
true
if successful, orfalse
if the file does not exist or any subdirectory could not be deleted.
-
deleteFileRecursively
Delete files recursively.- Returns:
true
if successful, orfalse
if 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:
true
if successful,false
if 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_file
denotes 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_file
denotes 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_dir
should be determined.- Returns:
- The path of
p_file
relative top_dir
, ornull
ifp_dir
is 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:
true
if the two file objects represent the same file in the file system, orfalse
otherwise.- 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.