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 SummaryModifier 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- 
writeFileWrites a stream to a file and then closes the same stream.- Parameters:
- in- the input stream
- filepath- the target file path
- Throws:
- IOException- I/O error
 
- 
writeFile- Throws:
- IOException
 
- 
writeFile
- 
readFile- Throws:
- IOException
 
- 
readFile- Throws:
- IOException
 
- 
appendFile
- 
computeDigest
- 
computeDigestThis 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
 
- 
computeDigestThis 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
 
- 
copyResourceWrites the specified classpath resource into a file- Parameters:
- resourceName- Fully qualified resource name
- out- The output file
- Throws:
- IOException- if the copy caused an error
 
- 
getExtensionGets the file extension- Parameters:
- fileName- name of the file
- Returns:
- the extension
 
- 
getBaseNameGets the file base name- Parameters:
- fileName- name of the file
- Returns:
- the base name
 
- 
getNameGets the file name excluding the path- Parameters:
- fileName- name or path of the file
- Returns:
- the file name
 
- 
getPathGets the path part- Parameters:
- fileName- name or path of the file
- Returns:
- the path
 
- 
getFolderSizeComputes 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
 
- 
getDisplaySizeRenders 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 rendered
- language- The language for the format symbols
- Returns:
- the size as human readable text
 
- 
getDisplaySizeKBRenders 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 rendered
- language- The language for the format symbols
- Returns:
- the size in KB as human readable text
 
- 
matchespublic 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 consider
- includes- 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
 
- 
matchesCheck if a given filename matches theincludesand not theexcludes- Parameters:
- filename- The filename to consider
- includes- 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
- 
toByteArraypublic static byte[] toByteArray(RandomAccessFile input, long start, long length) throws IOException - Throws:
- IOException
 
- 
toByteArray- Throws:
- IOException
 
- 
copyCopies the input file into the output at the given offset- Parameters:
- input- the file to copy
- output- the target file to copy to
- offset- an offset to use in copying from the input expressed in number of bytes
- Throws:
- IOException- raised in case of error
 
- 
replaceInFilepublic static void replaceInFile(String sourcePath, String token, String newValue) throws IOException - Throws:
- IOException
 
- 
copyFileQuickest way to copy a file in Java, makes use of NIO buffer.- Parameters:
- source- the source file to copy
- target- the target file
- Throws:
- IOException- if the copy resulted in an error
 
- 
deleteDeletes 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
 
- 
isInsideFolderChecks if file is in side folder at any level.- Parameters:
- folder- the folder to inspect
- file- the file to check
- Returns:
- true if file is in side folder
 
- 
createTempFileCreates 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 longsuffix
- suffix- 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
 
- 
createTempDirectoryCreates 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
 
 
-