Class FileTools


  • public class FileTools
    extends Object
    Author:
    keshav, Pavlidis, Will Braynen
    • Constructor Detail

      • FileTools

        public FileTools()
    • Method Detail

      • 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.
      • 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:
        File.delete()
      • 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:
        File.delete()
      • getExtension

        public static String getExtension​(String filename)
        Returns the extension of a file.
        Parameters:
        filename -
        Returns:
      • 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:
      • 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
      • 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