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 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 long
getFolderSize(File folder)
Computes the folder size as the sum of all files directly and indirectly contained.static 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)
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 Exception
- Throws:
Exception
-
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
-
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
-
-