Package com.logicaldoc.util.io
Class FileUtil
java.lang.Object
com.logicaldoc.util.io.FileUtil
This class manages I/O operations with files.
- Version:
- 4.0
- Author:
- Marco Meschieri - LogicalDOC
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendFile
(String text, String filepath) static String
computeDigest
(File file) This method calculates the digest of a file using the algorithm SHA-1.static String
static String
computeDigest
(String src) This method calculates the digest of a string using the algorithm SHA-1.static void
Copies the input file into the output at the given offsetstatic void
Quickest way to copy a file in Java, makes use of NIO buffer.static void
copyResource
(String resourceName, File out) Writes the specified classpath resource into a filestatic long
countLines
(File file) static File
createTempDirectory
(String prefix) Creates an empty folder in the default temporary-file directory, using the given prefix to generate its name.static File
createTempFile
(String prefix, String suffix) Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.static String
getBaseName
(String fileName) Gets the file base namestatic String
getDisplaySize
(long size, String language) Renders a file size in a more readable behaviour taking into account the user locale.static String
getDisplaySizeKB
(long size, String language) Renders a file size in a more readable behaviour taking into account the user locale.static String
getExtension
(String fileName) Gets the file extensionstatic long
getFolderSize
(File folder) Computes the folder size as the sum of all files directly and indirectly contained.static String
Gets the file name excluding the pathstatic String
Gets the path partstatic boolean
isDirEmpty
(Path directory) static boolean
isInsideFolder
(File folder, File file) Checks if file is in side folder at any level.static boolean
Check if a given filename matches theincludes
and not theexcludes
static boolean
matches
(String filename, Collection<String> includes, Collection<String> excludes) Check if a given filename matches theincludes
and not theexcludes
static void
static void
static void
moveQuitely
(File source, File target) static String
static String
static void
replaceInFile
(String sourcePath, String token, String newValue) static void
strongDelete
(File file) static byte[]
toByteArray
(File file) static byte[]
toByteArray
(File file, long start, long length) static byte[]
toByteArray
(RandomAccessFile input, long start, long length) static void
static void
writeFile
(InputStream in, String filepath) Writes a stream to a file and then closes the same stream.static void
static void
-
Method Details
-
writeFile
Writes a stream to a file and then closes the same stream.- Parameters:
in
- the input streamfilepath
- the target file path- Throws:
IOException
- I/O error
-
writeFile
- Throws:
IOException
-
writeFile
-
readFile
- Throws:
IOException
-
readFile
- Throws:
IOException
-
appendFile
-
computeDigest
-
computeDigest
This method calculates the digest of a file using the algorithm SHA-1.- Parameters:
file
- The file for which will be computed the digest- Returns:
- digest
-
computeDigest
This method calculates the digest of a string using the algorithm SHA-1.- Parameters:
src
- The string for which will be computed the digest- Returns:
- digest
-
copyResource
Writes the specified classpath resource into a file- Parameters:
resourceName
- Fully qualified resource nameout
- The output file- Throws:
IOException
- if the copy caused an error
-
getExtension
Gets the file extension- Parameters:
fileName
- name of the file- Returns:
- the extension
-
getBaseName
Gets the file base name- Parameters:
fileName
- name of the file- Returns:
- the base name
-
getName
Gets the file name excluding the path- Parameters:
fileName
- name or path of the file- Returns:
- the file name
-
getPath
Gets the path part- Parameters:
fileName
- name or path of the file- Returns:
- the path
-
getFolderSize
Computes the folder size as the sum of all files directly and indirectly contained.- Parameters:
folder
- the folder to calculate- Returns:
- the sum of the sizes of all contained files expressed in bytes
-
getDisplaySize
Renders a file size in a more readable behaviour taking into account the user locale. Depending on the size, the result will be presented in the following measure units: GB, MB, KB or Bytes- Parameters:
size
- Size to be renderedlanguage
- The language for the format symbols- Returns:
- the size as human readable text
-
getDisplaySizeKB
Renders a file size in a more readable behaviour taking into account the user locale. The size is always rendered in the KB(kilobyte) measure unit.- Parameters:
size
- Size to be renderedlanguage
- The language for the format symbols- Returns:
- the size in KB as human readable text
-
matches
public static boolean matches(String filename, Collection<String> includes, Collection<String> excludes) Check if a given filename matches theincludes
and not theexcludes
- Parameters:
filename
- The filename to considerincludes
- list of includes expressions (eg. *.doc,*dummy*)excludes
- list of excludes expressions (eg. *.doc,*dummy*)- Returns:
- true only if the passed filename matches the includes and not the excludes
-
matches
Check if a given filename matches theincludes
and not theexcludes
- Parameters:
filename
- The filename to considerincludes
- comma-separated list of includes expressions (eg. *.doc,*dummy*)excludes
- comma-separated list of excludes expressions (eg. *.doc,*dummy*)- Returns:
- true only if the passed filename matches the includes and not the excludes
-
writeUTF8
-
toByteArray
-
toByteArray
public static byte[] toByteArray(RandomAccessFile input, long start, long length) throws IOException - Throws:
IOException
-
toByteArray
- Throws:
IOException
-
copy
Copies the input file into the output at the given offset- Parameters:
input
- the file to copyoutput
- the target file to copy tooffset
- an offset to use in copying from the input expressed in number of bytes- Throws:
IOException
- raised in case of error
-
replaceInFile
public static void replaceInFile(String sourcePath, String token, String newValue) throws IOException - Throws:
IOException
-
copyFile
Quickest way to copy a file in Java, makes use of NIO buffer.- Parameters:
source
- the source file to copytarget
- the target file- Throws:
IOException
- if the copy resulted in an error
-
strongDelete
-
moveQuitely
-
isDirEmpty
- Throws:
IOException
-
merge
- Throws:
IOException
-
merge
- Throws:
IOException
-
split
- Throws:
IOException
-
countLines
- Throws:
IOException
-
isInsideFolder
Checks if file is in side folder at any level.- Parameters:
folder
- the folder to inspectfile
- the file to check- Returns:
- true if file is in side folder
-
createTempFile
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.- Parameters:
prefix
- The prefix string to be used in generating the file'sname; must be at least three characters longsuffixsuffix
- The suffix string to be used in generating the file'sname; may be null, in which case thesuffix ".tmp" will be used- Returns:
- An abstract pathname denoting a newly-created empty file
- Throws:
IOException
- If the file could not be created
-
createTempDirectory
Creates an empty folder in the default temporary-file directory, using the given prefix to generate its name.- Parameters:
prefix
- The prefix string to be used in generating the file'sname; must be at least three characters longsuffix- Returns:
- An abstract pathname denoting a newly-created empty folder
- Throws:
IOException
- If the directory could not be created
-