Class FileTools

java.lang.Object
ubic.basecode.util.FileTools

public class FileTools extends Object
Author:
keshav, Pavlidis, Will Braynen
  • Field Details

  • Constructor Details

    • FileTools

      public FileTools()
  • Method Details

    • addDataExtension

      public static String addDataExtension(String filename)
      Parameters:
      filename -
      Returns:
      the new filename with the added extension, but does not modify the filename parameter.
    • addImageExtension

      public static String addImageExtension(String filename)
      Parameters:
      filename -
      Returns:
      the new filename with the added extension, but does not modify the filename parameter.
    • changeExtension

      public static String changeExtension(String filename, String newExtension)
      Parameters:
      filename -
      newExtension -
      Returns:
      the new filename with the changed extension, but does not modify the filename parameter.
    • checkPathIsReadableFile

      public static void checkPathIsReadableFile(String file) throws IOException
      Parameters:
      file -
      Throws:
      IOException
    • chompExtension

      public static String chompExtension(String filename)
      Parameters:
      filename -
      Returns:
    • cleanForFileName

      public static String cleanForFileName(String name)
      Avoid getting file names with spaces, slashes, quotes, # etc; replace them with "_".
      Parameters:
      ee -
      Returns:
      Throws:
      IllegalArgumentException - if the resulting string is empty, or if the input is empty.
    • copy

      public static void copy(InputStream input, OutputStream output) throws IOException
      On completion streams are closed.
      Parameters:
      input -
      output -
      Throws:
      IOException
    • copyPlainOrCompressedFile

      public static File copyPlainOrCompressedFile(String sourcePath, String outputFilePath) throws FileNotFoundException, IOException
      Parameters:
      sourcePath -
      outputFilePath -
      Returns:
      Throws:
      FileNotFoundException
      IOException
    • createDir

      public static File createDir(String directory)
      Creates the directory if it does not exist.
      Parameters:
      directory -
      Returns:
    • deleteDir

      public static int deleteDir(File directory)
      Deletes the directory and subdirectories if empty.
      Parameters:
      directory -
      Returns:
      int The number of directories deleted.
      See Also:
    • deleteFiles

      public static int deleteFiles(Collection<File> files)
      Deletes the specified Collection of files.
      Parameters:
      files -
      Returns:
      int The number of files deleted.
      See Also:
    • getExtension

      public static String getExtension(String filename)
      Returns the extension of a file.
      Parameters:
      filename -
      Returns:
    • getInputStreamFromPlainOrCompressedFile

      public static InputStream getInputStreamFromPlainOrCompressedFile(String fileName) throws IOException, FileNotFoundException
      Open a non-compresed, zipped, or gzipped file. Uses the file name pattern to figure this out.
      Parameters:
      fileName - . If Zipped, only the first file in the archive is used.
      Returns:
      Throws:
      IOException
      FileNotFoundException
    • getLines

      public static List<String> getLines(File file) throws IOException
      opens a file and returns its contents as a list of lines.
      Returns:
      - List of strings representing the lines, first line is first in list
      Throws:
      IOException
    • getLines

      public static List<String> getLines(String filename) throws IOException
      opens a file and returns its contents as a list of lines.
      Returns:
      - List of strings representing the lines, first line is first in list
      Throws:
      IOException
    • getStringListFromFile

      public static List<String> getStringListFromFile(File f) throws Exception
      Used for reading output generated by Collection.toString(). For example [a,b,c] stored in a file would be converted to a new List containing "a", "b" and "c".

      Warning this relies on behaviour of other API's toString.

      Parameters:
      f - - input file, with only one line for the toString output.
      Returns:
      - list created from the strings in the file
      Throws:
      Exception
    • hasImageExtension

      public static boolean hasImageExtension(String filename)
      Parameters:
      filename -
      Returns:
    • hasXMLExtension

      public static boolean hasXMLExtension(String filename)
      Parameters:
      filename -
      Returns:
    • isGZipped

      public static boolean isGZipped(String fileName)
      Parameters:
      fileName -
      Returns:
    • isZipped

      public static boolean isZipped(String filename)
      Parameters:
      filename -
      Returns:
    • listDirectoryFiles

      public static Collection<File> listDirectoryFiles(File directory)
      Given a File object representing a directory, return a collection of File objects representing the files contained in that directory.
      Parameters:
      directory -
      Returns:
    • listSubDirectories

      public static Collection<File> listSubDirectories(File directory)
      Given a File object representing a directory, return a collection of File objects representing the directories contained in that directory.
      Parameters:
      directory -
      Returns:
    • resourceToPath

      public static String resourceToPath(String resourcePath) throws URISyntaxException
      Parameters:
      resourcePath -
      Returns:
      Throws:
      URISyntaxException
    • stringsToFile

      public static void stringsToFile(Collection<String> lines, File f) throws Exception
      Outputs a many strings to a file, one line at a time.
      Throws:
      Exception
    • stringsToFile

      public static void stringsToFile(Collection<String> lines, File f, boolean append) throws Exception
      Outputs many strings to a file, one line at a time.
      Parameters:
      lines - - input lines
      f - - file that wrote to
      append - - add to end of file or overwrite
      Throws:
      Exception
    • stringsToFile

      public static void stringsToFile(Collection<String> lines, String f) throws Exception
      Outputs a many strings to a file, one line at a time.
      Throws:
      Exception
    • stringToFile

      public static void stringToFile(String s, File f) throws Exception
      Outputs a string to a file.
      Throws:
      Exception
    • stringToFile

      public static void stringToFile(String s, File f, boolean append) throws Exception
      Outputs a string to a file, one line at a time.
      Parameters:
      s - - input line/string
      f - - file that wrote to
      append - - add to end of file or overwrite
      Throws:
      Exception
    • testDir

      public static boolean testDir(String dirname)
      Parameters:
      dirname - directory name
      Returns:
    • testFile

      public static boolean testFile(File file)
      Test whether a File is writeable.
      Parameters:
      file -
      Returns:
    • testFile

      public static boolean testFile(String filename)
      Parameters:
      filename -
      Returns:
    • touch

      public static void touch(File f) throws IOException
      Create or update the modification date of the given file. If the file does not exist, create it.
      Parameters:
      f -
      Throws:
      IOException
    • unGzipFile

      public static String unGzipFile(String seekFile) throws IOException
      Given the path to a gzipped-file, unzips it into the same directory. If the file already exists it will be overwritten.
      Parameters:
      seekFile -
      Returns:
      path to the unzipped file.
      Throws:
      IOException
    • unZipFiles

      public static Collection<File> unZipFiles(String seekFile) throws IOException
      Parameters:
      seekFile -
      Returns:
      Collection of File objects
      Throws:
      IOException