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 StringaddDataExtension(String filename) static StringaddImageExtension(String filename) static StringchangeExtension(String filename, String newExtension) static voidstatic StringchompExtension(String filename) static StringcleanForFileName(String name) Avoid getting file names with spaces, slashes, quotes, # etc; replace them with "_".static voidcopy(InputStream input, OutputStream output) On completion streams are closed.static FilecopyPlainOrCompressedFile(String sourcePath, String outputFilePath) static FileCreates the directory if it does not exist.static intDeletes the directory and subdirectories if empty.static intdeleteFiles(Collection<File> files) Deletes the specified Collection of files.static StringgetExtension(String filename) Returns the extension of a file.static InputStreamOpen 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 booleanhasImageExtension(String filename) static booleanhasXMLExtension(String filename) static booleanstatic booleanstatic 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 StringresourceToPath(String resourcePath) static voidstringsToFile(Collection<String> lines, File f) Outputs a many strings to a file, one line at a time.static voidstringsToFile(Collection<String> lines, File f, boolean append) Outputs many strings to a file, one line at a time.static voidstringsToFile(Collection<String> lines, String f) Outputs a many strings to a file, one line at a time.static voidstringToFile(String s, File f) Outputs a string to a file.static voidstringToFile(String s, File f, boolean append) Outputs a string to a file, one line at a time.static booleanstatic booleanTest whether a File is writeable.static booleanstatic voidCreate or update the modification date of the given file.static StringunGzipFile(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
filenameparameter.
-
addImageExtension
- Parameters:
filename-- Returns:
- the new filename with the added extension, but does not modify the
filenameparameter.
-
changeExtension
- Parameters:
filename-newExtension-- Returns:
- the new filename with the changed extension, but does not modify the
filenameparameter.
-
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:
FileNotFoundExceptionIOException
-
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:
IOExceptionFileNotFoundException
-
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
-