Package ubic.basecode.util
Class FileTools
- java.lang.Object
-
- ubic.basecode.util.FileTools
-
public class FileTools extends Object
- Author:
- keshav, Pavlidis, Will Braynen
-
-
Field Summary
Fields Modifier and Type Field Description protected static String[]
DATA_EXTENSIONS
static String
DEFAULT_DATA_EXTENSION
static String
DEFAULT_IMAGE_EXTENSION
static String
DEFAULT_XML_EXTENSION
protected static String
GIF_EXTENSION
protected static String[]
IMAGE_EXTENSIONS
protected static String[]
XML_EXTENSIONS
-
Constructor Summary
Constructors Constructor Description FileTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
addDataExtension(String filename)
static String
addImageExtension(String filename)
static String
changeExtension(String filename, String newExtension)
static void
checkPathIsReadableFile(String file)
static String
chompExtension(String filename)
static String
cleanForFileName(String name)
Avoid getting file names with spaces, slashes, quotes, # etc; replace them with "_".static void
copy(InputStream input, OutputStream output)
On completion streams are closed.static File
copyPlainOrCompressedFile(String sourcePath, String outputFilePath)
static File
createDir(String directory)
Creates the directory if it does not exist.static int
deleteDir(File directory)
Deletes the directory and subdirectories if empty.static int
deleteFiles(Collection<File> files)
Deletes the specified Collection of files.static String
getExtension(String filename)
Returns the extension of a file.static InputStream
getInputStreamFromPlainOrCompressedFile(String fileName)
Open a non-compresed, zipped, or gzipped file.static List<String>
getLines(File file)
opens a file and returns its contents as a list of lines.static List<String>
getLines(String filename)
opens a file and returns its contents as a list of lines.static List<String>
getStringListFromFile(File f)
Used for reading output generated by Collection.toString().static boolean
hasImageExtension(String filename)
static boolean
hasXMLExtension(String filename)
static boolean
isGZipped(String fileName)
static boolean
isZipped(String filename)
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.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.static String
resourceToPath(String resourcePath)
static void
stringsToFile(Collection<String> lines, File f)
Outputs a many strings to a file, one line at a time.static void
stringsToFile(Collection<String> lines, File f, boolean append)
Outputs many strings to a file, one line at a time.static void
stringsToFile(Collection<String> lines, String f)
Outputs a many strings to a file, one line at a time.static void
stringToFile(String s, File f)
Outputs a string to a file.static void
stringToFile(String s, File f, boolean append)
Outputs a string to a file, one line at a time.static boolean
testDir(String dirname)
static boolean
testFile(File file)
Test whether a File is writeable.static boolean
testFile(String filename)
static void
touch(File f)
Create or update the modification date of the given file.static String
unGzipFile(String seekFile)
Given the path to a gzipped-file, unzips it into the same directory.static Collection<File>
unZipFiles(String seekFile)
-
-
-
Field Detail
-
DEFAULT_DATA_EXTENSION
public static final String DEFAULT_DATA_EXTENSION
- See Also:
- Constant Field Values
-
DEFAULT_IMAGE_EXTENSION
public static final String DEFAULT_IMAGE_EXTENSION
- See Also:
- Constant Field Values
-
DEFAULT_XML_EXTENSION
public static final String DEFAULT_XML_EXTENSION
- See Also:
- Constant Field Values
-
DATA_EXTENSIONS
protected static final String[] DATA_EXTENSIONS
-
GIF_EXTENSION
protected static final String GIF_EXTENSION
- See Also:
- Constant Field Values
-
IMAGE_EXTENSIONS
protected static final String[] IMAGE_EXTENSIONS
-
XML_EXTENSIONS
protected static final String[] XML_EXTENSIONS
-
-
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.
-
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:
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:
-
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 linesf
- - file that wrote toappend
- - 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/stringf
- - file that wrote toappend
- - 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
-
-