Class FileUtils

java.lang.Object
uk.ac.gda.util.io.FileUtils

public final class FileUtils extends Object
A class with a collection of file management static utility classes. Several method contents copied from code snippets available on the web.
  • Field Details

    • BOM

      public static final char BOM
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • recursiveDelete

      public static final boolean recursiveDelete(File parent)
      Parameters:
      parent -
      Returns:
      boolean
    • deleteContents

      public static final void deleteContents(File parent)
      Parameters:
      parent -
    • getUnique

      public static File getUnique(File dir, String template, String ext)
      Generates a unique file of the name template or template+an integer
      Parameters:
      dir -
      template -
      ext -
      Returns:
      a unique file.
    • getUnique

      public static File getUnique(File dir, String template, String ext, int i)
      Parameters:
      dir -
      template -
      ext -
      i -
      Returns:
      file
    • recursiveDeleteOnExit

      public static final void recursiveDeleteOnExit(File parent)
      Recursively delete parent folder on exit of JVM
      Parameters:
      parent -
    • recursiveCopy

      public static final void recursiveCopy(File source_dir, File destination_dir) throws IOException
      Recursively copy one folder to another Deleting the contents of the destination folder before copying. Use at your peril!
      Parameters:
      source_dir -
      destination_dir -
      Throws:
      IOException
    • recursiveCopyNio

      public static final void recursiveCopyNio(File source_dir, File destination_dir) throws IOException
      Recursively copy one folder to another Deleting the contents of the destination folder before copying. Use at your peril!
      Parameters:
      source_dir -
      destination_dir -
      Throws:
      IOException
    • recursiveCopyNioNoDelete

      public static final void recursiveCopyNioNoDelete(File source_dir, File destination_dir) throws IOException
      Recursively copy one folder to another Not deleting the contents of the destination folder before copying. Any file that already exists will not be copied.
      Parameters:
      source_dir -
      destination_dir -
      Throws:
      IOException
    • recursiveIncrementalCopy

      public static final void recursiveIncrementalCopy(File source_dir, File destination_dir) throws IOException
      Recursively copy one folder to another, but not deleting destination data. Only copies files if timestamp indicates the data is newer. inf[0] - returns files copied, can be used to track progress inf[1] - returns files skipped, can be used to track progress inf[2] - if set to -1 will halt copy
      Parameters:
      source_dir -
      destination_dir -
      Throws:
      IOException
    • recursiveIncrementalCopy

      public static final void recursiveIncrementalCopy(File source_dir, File destination_dir, int[] inf) throws IOException
      Recursively copy one folder to another, but not deleting destination data. Only copies files if timestamp indicates the data is newer. inf[0] - returns files copied, can be used to track progress inf[1] - returns files skipped, can be used to track progress inf[2] - if set to -1 will halt copy
      Parameters:
      source_dir -
      destination_dir -
      inf -
      Throws:
      IOException
    • copy

      public static final void copy(File source_file, String destination_dir) throws IOException
      Parameters:
      source_file -
      destination_dir -
      Throws:
      IOException
    • copy

      public static final void copy(File source_file, File destination_file) throws IOException
      Overwrites destination_file if it exists, creates new if not.
      Parameters:
      source_file -
      destination_file -
      Throws:
      IOException
    • copy

      public static final void copy(File source_file, File destination_file, byte[] buffer) throws IOException
      Overwrites destination_file if it exists, creates new if not.
      Parameters:
      source_file -
      destination_file -
      buffer -
      Throws:
      IOException
    • copyNio

      public static final void copyNio(File source_file, File destination_file) throws IOException
      Parameters:
      source_file -
      destination_file -
      Throws:
      IOException
    • copyNioNoCopyOver

      public static final void copyNioNoCopyOver(File source_file, File destination_file) throws IOException
      Parameters:
      source_file -
      destination_file -
      Throws:
      IOException
    • write

      public static final void write(BufferedInputStream source, File destination_file) throws IOException
      Parameters:
      source -
      destination_file -
      Throws:
      IOException
    • write

      public static final void write(InputStream source_raw, OutputStream destination_raw) throws IOException
      Parameters:
      source_raw -
      destination_raw -
      Throws:
      IOException
    • getByteArrayFromStream

      public static final byte[] getByteArrayFromStream(InputStream source) throws IOException
      Parameters:
      source -
      Returns:
      byte[]
      Throws:
      IOException
    • getByteArrayFromStream

      public static final byte[] getByteArrayFromStream(InputStream source, boolean shouldClose) throws IOException
      Parameters:
      source -
      shouldClose -
      Returns:
      byte[]
      Throws:
      IOException
    • getByteStream

      public static final ByteArrayOutputStream getByteStream(InputStream source) throws IOException
      Parameters:
      source -
      Returns:
      ByteArrayOutputStream
      Throws:
      IOException
    • getByteStream

      public static final ByteArrayOutputStream getByteStream(InputStream source, boolean shouldClose) throws IOException
      Parameters:
      source -
      shouldClose -
      Returns:
      ByteArrayOutputStream
      Throws:
      IOException
    • readFile

      public static final StringBuffer readFile(File file) throws Exception
      Parameters:
      file -
      Returns:
      StringBuffer
      Throws:
      Exception
    • readFile

      public static final StringBuffer readFile(InputStream in) throws Exception
      Parameters:
      in -
      Returns:
      StringBuffer
      Throws:
      Exception
    • readFile

      public static final StringBuffer readFile(InputStream in, String charsetName) throws Exception
      Parameters:
      in -
      charsetName -
      Returns:
      StringBuffer
      Throws:
      Exception
    • readFileAsList

      public static final List<String> readFileAsList(File f) throws Exception
      Parameters:
      f -
      Returns:
      List
      Throws:
      Exception
    • readFileAsList

      public static final List<String> readFileAsList(File file, String encodingOfFile, boolean removeBom, boolean trimLines) throws IOException
      Reads a file using the encoding parameter passed and returns each line as an item in the result list Optionally: the BOM can be removed (first character of file if present) the line can be trimmed of whitespace
      Parameters:
      file - The file to read
      encodingOfFile - The encoding format the file should be read as
      removeBom - Whether the BOM should be removed, if present.
      trimLines - If each line should have leading and trailing whitespace removed
      Returns:
      List lines of the file optionally trimmed
      Throws:
      IOException - If the file can not be read
    • isUnix

      public static boolean isUnix(String stringToTest)
      This method returns true if file is Unix and false if Windows line endings.
      Parameters:
      stringToTest - The text of the file
      Returns:
      boolean true if the file is Unix.
    • isBOM

      public static boolean isBOM(String stringToTest)
      Returns true if the string is starting with a BOM
      Parameters:
      stringToTest -
      Returns:
      boolean
    • write

      public static void write(File file, String text, String encoding) throws Exception
      Parameters:
      file -
      text -
      encoding -
      Throws:
      Exception
    • write

      public static void write(File file, String text) throws Exception
      This method attempts to write a string to file in US-ASCII. The code was moved directly from atos.SaveTextFile and no check has been made to how efficient it is.
      Parameters:
      file -
      text -
      Throws:
      Exception
    • write

      public static void write(OutputStream out, String text, String charset, boolean addBomIfNeeded) throws Exception
      This method writes to a stream a potentially large String. The current limit for Java servlets in 90Mb with this method there is a workaround in Java 1.5 for this problem: http://forum.java.sun.com/thread.jspa?threadID=418441invalid input: '&messageID'=2816084 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026745
      Parameters:
      out -
      text -
      charset -
      addBomIfNeeded -
      Throws:
      Exception
    • write

      public static void write(File file, List<String> list) throws Exception
      Parameters:
      file -
      list -
      Throws:
      Exception
    • createNewUniqueDir

      public static File createNewUniqueDir(File file)
      Parameters:
      file -
      Returns:
      File
    • createNewUniqueDir

      public static File createNewUniqueDir(File file, String templateName)
      Parameters:
      file -
      templateName -
      Returns:
      File
    • getParentDirName

      public static final String getParentDirName(String name)
      Parameters:
      name -
      Returns:
      String
    • writeToFile

      public static final void writeToFile(File file, InputStream fromNoClose) throws Exception
      Parameters:
      file -
      fromNoClose -
      Throws:
      Exception
    • getDiskSpace

      public static long getDiskSpace(File dir)
      Parameters:
      dir -
      Returns:
      long
    • isDiskSpaceAvaliableMB

      public static boolean isDiskSpaceAvaliableMB(File tmp, long sizeInMB) throws IOException
      Parameters:
      tmp -
      sizeInMB -
      Returns:
      boolean
      Throws:
      IOException
    • isDiskSpaceAvaliable

      public static boolean isDiskSpaceAvaliable(File tmp, long sizeInB) throws IOException
      Parameters:
      tmp -
      sizeInB -
      Returns:
      boolean
      Throws:
      IOException
    • getFileSizeRecursive

      public static long getFileSizeRecursive(File file, boolean subFolders)
      Parameters:
      file -
      subFolders -
      Returns:
      long
    • getFileExtension

      public static String getFileExtension(String fileName)
      Get File extension (result will NOT include ".")
      Parameters:
      fileName -
      Returns:
      String file extension value, or "" is no extension
    • getFileExtension

      public static String getFileExtension(File file)
      Get File extension (result will NOT include ".")
      Parameters:
      file -
      Returns:
      String file extension value, or "" is no extension
    • getFileNameNoExtension

      public static String getFileNameNoExtension(File file)
      Get Filename minus it's extension if present
      Parameters:
      file - File to get filename from
      Returns:
      String filename minus its extension
    • formatSize

      public static String formatSize(long longSize, int decimalPos)
      Formats a file size
      Parameters:
      longSize -
      decimalPos -
      Returns:
      formatted string for size.
    • getSystemInfo

      public static String getSystemInfo(File file)
      Gets the last modified and size formatted as a string
      Parameters:
      file -
      Returns:
      date and size