Class IoUtil

java.lang.Object
com.compomics.util.io.IoUtil

public class IoUtil
extends Object
Utils for I/O and file handling.
Author:
Marc Vaudel, Harald Barsnes
  • Field Details

  • Constructor Details

  • Method Details

    • emptyDir

      public static boolean emptyDir​(File dir)
      Deletes all files and subdirectories under dir. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.
      Parameters:
      dir - the directory to delete
      Returns:
      rue if all deletions were successful
    • deleteDir

      public static boolean deleteDir​(File dir)
      Deletes all files and subdirectories under dir and dir itself. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.
      Parameters:
      dir - the directory to delete
      Returns:
      rue if all deletions were successful
    • copyFile

      public static void copyFile​(File in, File out) throws IOException
      Copy the content of a file to another.
      Parameters:
      in - the file to copy from
      out - the file to copy to
      Throws:
      IOException - if a problem occurs when writing to the file
    • append

      public static void append​(File in, File out) throws IOException
      Appends the content of a file to another.
      Parameters:
      in - the file to copy from
      out - the file to copy to
      Throws:
      IOException - if a problem occurs when writing to the file
    • copyFile

      public static void copyFile​(File in, File out, boolean overwrite) throws IOException
      Copy the content of one file to another.
      Parameters:
      in - the file to copy from
      out - the file to copy to
      overwrite - boolean indicating whether out should be overwritten
      Throws:
      IOException - if an error occurred while reading or writing a file
    • getFileName

      public static String getFileName​(String filePath)
      An OS independent getName alternative. Useful if the path is provided as a hardcoded string and opened in a different OS.
      Parameters:
      filePath - the file path as a string
      Returns:
      the file name, or the complete path of no file name is detected
    • getFileName

      public static String getFileName​(File file)
      An OS independent getName alternative. Useful if the path is provided as a hardcoded string and opened in a different OS.
      Parameters:
      file - the file
      Returns:
      the file name, or the complete path of no file name is detected
    • getExtension

      public static String getExtension​(File file)
      Returns the extensions of a file.
      Parameters:
      file - the file
      Returns:
      the extension of a file
    • getExtension

      public static String getExtension​(String fileName)
      Returns the extensions of a file name.
      Parameters:
      fileName - The file name.
      Returns:
      The extension of the file name.
    • getFilenameExtensionLowerCase

      public static String getFilenameExtensionLowerCase​(String fileName)
      Returns the given file name with lower-case extension.
      Parameters:
      fileName - The name of the file.
      Returns:
      The name of the file with lower-case extension.
    • existsExtensionNotCaseSensitive

      public static File existsExtensionNotCaseSensitive​(File file)
      Checks if the given file exists with the extension in another case and returns it. Returns the given file otherwise.
      Parameters:
      file - The file to check.
      Returns:
      The existing file with another extension if it exists, the given file otherwise.
    • appendSuffix

      public static String appendSuffix​(String fileName, String suffix)
      Appends a suffix to a file name before the file extension.
      Parameters:
      fileName - the file name
      suffix - the suffix to add
      Returns:
      the file name with suffix
    • removeExtension

      public static String removeExtension​(String fileName)
      Removes the extension from a file name or path.
      Parameters:
      fileName - the file name
      Returns:
      the file name without extension
    • saveUrl

      public static File saveUrl​(File saveFile, String targetUrlAsString, int fileSizeInBytes, String userName, String password, WaitingHandler waitingHandler) throws MalformedURLException, IOException, FileNotFoundException
      Save a file from a URL.
      Parameters:
      saveFile - the file to save to
      targetUrlAsString - the target URL as a string
      fileSizeInBytes - the file size in bytes
      userName - the user name
      password - the password
      waitingHandler - the waiting handler
      Returns:
      the saved file
      Throws:
      MalformedURLException - thrown if an MalformedURLException occurs
      IOException - thrown if an IOException occurs
      FileNotFoundException - thrown if a FileNotFoundException occurs
    • checkIfURLExists

      public static boolean checkIfURLExists​(String targetUrlAsString, String userName, String password)
      Check if a given URL exists.
      Parameters:
      targetUrlAsString - the URL to check
      userName - the user name
      password - the password
      Returns:
      true of it exists
    • getFileSize

      public static int getFileSize​(URL url)
      Returns the size of the file located at the given URL.
      Parameters:
      url - the url of the file
      Returns:
      the size of the file
    • closeBuffer

      public static void closeBuffer​(MappedByteBuffer buffer)
      Attempts at closing a buffer. Taken from https://stackoverflow.com/questions/2972986/how-to-unmap-a-file-from-memory-mapped-using-filechannel-in-java.
      Parameters:
      buffer - the buffer to close