Package com.logicaldoc.util.io
Class FileUtil
- java.lang.Object
-
- com.logicaldoc.util.io.FileUtil
-
public class FileUtil extends Object
This class manages I/O operations with files.- Version:
- 4.0
- Author:
- Marco Meschieri - LogicalDOC
-
-
Constructor Summary
Constructors Constructor Description FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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
computeDigest(InputStream is)
static String
computeDigest(String src)
This method calculates the digest of a string using the algorithm SHA-1.static byte[]
computeSha1Hash(File file)
This method calculates the digest of a file using the algorithm SHA-1.static byte[]
computeSha1Hash(InputStream is)
This method calculates the digest of a inputStram content using the algorithm SHA-1.static void
copy(File input, File output, long offset)
Copies the input file into the output at the given offsetstatic void
copyFile(File source, File target)
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
createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, usingthe 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
getName(String fileName)
Gets the file name excluding the pathstatic String
getPath(String fileName)
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
matches(String filename, String[] includes, String[] excludes)
Check if a given filename matches theincludes
and not theexcludes
static boolean
matches(String filename, String includes, String excludes)
Check if a given filename matches theincludes
and not theexcludes
static void
merge(File f1, File f2, File merged)
static void
merge(List<File> files, File merged)
static String
readFile(File file)
static String
readFile(String filePath)
static void
replaceInFile(String sourcePath, String token, String newValue)
static List<File>
split(File file, long chunkSize, File destDir)
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
writeFile(byte[] in, String filepath)
static void
writeFile(InputStream in, String filepath)
Writes a stream to a file and then closes the same stream.static void
writeFile(String text, String filepath)
static void
writeUTF8(String content, File file, boolean append)
-
-
-
Method Detail
-
writeFile
public static void writeFile(InputStream in, String filepath) throws FileNotFoundException, IOException
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 errorFileNotFoundException
- Unexisting output file
-
writeFile
public static void writeFile(byte[] in, String filepath) throws Exception
- Throws:
Exception
-
readFile
public static String readFile(File file) throws IOException
- Throws:
IOException
-
readFile
public static String readFile(String filePath) throws IOException
- Throws:
IOException
-
computeDigest
public static String computeDigest(InputStream is)
-
computeDigest
public static String computeDigest(File file)
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
public static String computeDigest(String src)
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
-
computeSha1Hash
public static byte[] computeSha1Hash(File file)
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
-
computeSha1Hash
public static byte[] computeSha1Hash(InputStream is)
This method calculates the digest of a inputStram content using the algorithm SHA-1.- Parameters:
is
- The content of which will be computed the digest- Returns:
- digest
-
copyResource
public static void copyResource(String resourceName, File out) throws IOException
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
public static String getExtension(String fileName)
Gets the file extension- Parameters:
fileName
- name of the file- Returns:
- the extension
-
getBaseName
public static String getBaseName(String fileName)
Gets the file base name- Parameters:
fileName
- name of the file- Returns:
- the base name
-
getName
public static String getName(String fileName)
Gets the file name excluding the path- Parameters:
fileName
- name or path of the file- Returns:
- the file name
-
getPath
public static String getPath(String fileName)
Gets the path part- Parameters:
fileName
- name or path of the file- Returns:
- the path
-
getFolderSize
public static long getFolderSize(File folder)
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
public static String getDisplaySize(long size, String language)
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
public static String getDisplaySizeKB(long size, String language)
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, String[] includes, 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
public static boolean matches(String filename, String includes, String excludes)
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
-
toByteArray
public static byte[] toByteArray(File file)
-
toByteArray
public static byte[] toByteArray(RandomAccessFile input, long start, long length) throws IOException
- Throws:
IOException
-
toByteArray
public static byte[] toByteArray(File file, long start, long length) throws IOException
- Throws:
IOException
-
copy
public static void copy(File input, File output, long offset) throws IOException
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 Exception
- Throws:
Exception
-
copyFile
public static void copyFile(File source, File target) throws IOException
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
public static void strongDelete(File file)
-
isDirEmpty
public static boolean isDirEmpty(Path directory) throws IOException
- Throws:
IOException
-
merge
public static void merge(File f1, File f2, File merged) throws IOException
- Throws:
IOException
-
merge
public static void merge(List<File> files, File merged) throws IOException
- Throws:
IOException
-
split
public static List<File> split(File file, long chunkSize, File destDir) throws IOException
- Throws:
IOException
-
countLines
public static long countLines(File file) throws IOException
- Throws:
IOException
-
isInsideFolder
public static boolean isInsideFolder(File folder, File file)
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
public static File createTempFile(String prefix, String suffix) throws IOException
Creates an empty file in the default temporary-file directory, usingthe given prefix and suffix to generate its name. Invoking this methodis equivalent to invoking- 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 a file could not be created
-
-