Package ubic.basecode.util
Class FileTools
java.lang.Object
ubic.basecode.util.FileTools
- Author:
- keshav, Pavlidis, Will Braynen
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
addDataExtension
(String filename) static String
addImageExtension
(String filename) static String
changeExtension
(String filename, String newExtension) static void
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
Creates the directory if it does not exist.static int
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
Open a non-compresed, zipped, or gzipped file.opens a file and returns its contents as a list of lines.opens a file and returns its contents as a list of lines.Used for reading output generated by Collection.toString().static boolean
hasImageExtension
(String filename) static boolean
hasXMLExtension
(String filename) static boolean
static boolean
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
static boolean
Test whether a File is writeable.static boolean
static void
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 Details
-
DEFAULT_DATA_EXTENSION
- See Also:
-
DEFAULT_IMAGE_EXTENSION
- See Also:
-
DEFAULT_XML_EXTENSION
- See Also:
-
DATA_EXTENSIONS
-
GIF_EXTENSION
- See Also:
-
IMAGE_EXTENSIONS
-
XML_EXTENSIONS
-
-
Constructor Details
-
FileTools
public FileTools()
-
-
Method Details
-
addDataExtension
- Parameters:
filename
-- Returns:
- the new filename with the added extension, but does not modify the
filename
parameter.
-
addImageExtension
- Parameters:
filename
-- Returns:
- the new filename with the added extension, but does not modify the
filename
parameter.
-
changeExtension
- Parameters:
filename
-newExtension
-- Returns:
- the new filename with the changed extension, but does not modify the
filename
parameter.
-
checkPathIsReadableFile
- Parameters:
file
-- Throws:
IOException
-
chompExtension
- Parameters:
filename
-- Returns:
-
cleanForFileName
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
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
Creates the directory if it does not exist.- Parameters:
directory
-- Returns:
-
deleteDir
Deletes the directory and subdirectories if empty.- Parameters:
directory
-- Returns:
- int The number of directories deleted.
- See Also:
-
deleteFiles
Deletes the specified Collection of files.- Parameters:
files
-- Returns:
- int The number of files deleted.
- See Also:
-
getExtension
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
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
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
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
- Parameters:
filename
-- Returns:
-
hasXMLExtension
- Parameters:
filename
-- Returns:
-
isGZipped
- Parameters:
fileName
-- Returns:
-
isZipped
- Parameters:
filename
-- Returns:
-
listDirectoryFiles
Given a File object representing a directory, return a collection of File objects representing the files contained in that directory.- Parameters:
directory
-- Returns:
-
listSubDirectories
Given a File object representing a directory, return a collection of File objects representing the directories contained in that directory.- Parameters:
directory
-- Returns:
-
resourceToPath
- Parameters:
resourcePath
-- Returns:
- Throws:
URISyntaxException
-
stringsToFile
Outputs a many strings to a file, one line at a time.- Throws:
Exception
-
stringsToFile
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
Outputs a many strings to a file, one line at a time.- Throws:
Exception
-
stringToFile
Outputs a string to a file.- Throws:
Exception
-
stringToFile
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
- Parameters:
dirname
- directory name- Returns:
-
testFile
Test whether a File is writeable.- Parameters:
file
-- Returns:
-
testFile
- Parameters:
filename
-- Returns:
-
touch
Create or update the modification date of the given file. If the file does not exist, create it.- Parameters:
f
-- Throws:
IOException
-
unGzipFile
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
- Parameters:
seekFile
-- Returns:
- Collection of File objects
- Throws:
IOException
-