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
  • Method Details

    • writeFile

      public static void writeFile(InputStream in, String filepath) throws IOException
      Writes 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

      public static void writeFile(byte[] in, String filepath) throws IOException
      Throws:
      IOException
    • writeFile

      public static void writeFile(String text, String filepath)
    • readFile

      public static String readFile(File file) throws IOException
      Throws:
      IOException
    • readFile

      public static String readFile(String filePath) throws IOException
      Throws:
      IOException
    • appendFile

      public static void appendFile(String text, String filepath)
    • 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
    • copyResource

      public static void copyResource(String resourceName, File out) throws IOException
      Writes 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
    • 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 rendered
      language - 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 rendered
      language - 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 the includes and not the excludes
      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
    • matches

      public static boolean matches(String filename, String includes, String excludes)
      Check if a given filename matches the includes and not the excludes
      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

      public static void writeUTF8(String content, File file, boolean append)
    • 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 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
    • replaceInFile

      public static void replaceInFile(String sourcePath, String token, String newValue) throws IOException
      Throws:
      IOException
    • 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 copy
      target - the target file
      Throws:
      IOException - if the copy resulted in an error
    • strongDelete

      public static void strongDelete(File file)
    • moveQuitely

      public static void moveQuitely(File source, File target)
    • 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 inspect
      file - 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, 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
    • createTempDirectory

      public static File createTempDirectory(String prefix) throws IOException
      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