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 voidappendFile(String text, String filepath) static StringcomputeDigest(File file) This method calculates the digest of a file using the algorithm SHA-1.static Stringstatic StringcomputeDigest(String src) This method calculates the digest of a string using the algorithm SHA-1.static voidCopies the input file into the output at the given offsetstatic voidQuickest way to copy a file in Java, makes use of NIO buffer.static voidcopyResource(String resourceName, File out) Writes the specified classpath resource into a filestatic longcountLines(File file) static FilecreateTempDirectory(String prefix) Creates an empty folder in the default temporary-file directory, using the given prefix to generate its name.static FilecreateTempFile(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 booleanDeletes a file doing the best effortstatic StringgetBaseName(String fileName) Gets the file base namestatic StringgetDisplaySize(long size, String language) Renders a file size in a more readable behaviour taking into account the user locale.static StringgetDisplaySizeKB(long size, String language) Renders a file size in a more readable behaviour taking into account the user locale.static StringgetExtension(String fileName) Gets the file extensionstatic longgetFolderSize(File folder) Computes the folder size as the sum of all files directly and indirectly contained.static StringGets the file name excluding the pathstatic StringGets the path partstatic booleanisDirEmpty(Path directory) static booleanisInsideFolder(File folder, File file) Checks if file is in side folder at any level.static booleanCheck if a given filename matches theincludesand not theexcludesstatic booleanmatches(String filename, Collection<String> includes, Collection<String> excludes) Check if a given filename matches theincludesand not theexcludesstatic voidstatic voidstatic voidmoveQuitely(File source, File target) static Stringstatic Stringstatic voidreplaceInFile(String sourcePath, String token, String newValue) static byte[]toByteArray(File file) static byte[]toByteArray(File file, long start, long length) static byte[]toByteArray(RandomAccessFile input, long start, long length) static voidstatic voidwriteFile(InputStream in, String filepath) Writes a stream to a file and then closes the same stream.static voidstatic 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 theincludesand 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 theincludesand 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
-
delete
Deletes a file doing the best effort- Parameters:
file- the file to delete- Returns:
- true inly if the deletion has been done successfully
-
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 folder's name; 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
-