|
TUM CCSM Commons | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.tum.cs.commons.filesystem.FileSystemUtils
public class FileSystemUtils
File system utilities.
| Field Summary | |
|---|---|
static Charset |
UTF8_CHARSET
Charset for UTF-8. |
static String |
UTF8_ENCODING
Encoding for UTF-8. |
| Constructor Summary | |
|---|---|
FileSystemUtils()
|
|
| Method Summary | |
|---|---|
static InputStream |
autoDecompressStream(InputStream in)
Transparently creates a stream for decompression if the provided stream is compressed. |
static File |
canonize(File file)
Deprecated. |
static String |
canonize(String path)
Deprecated. |
static void |
close(Closeable closeable)
Convenience method for calling close(Closeable, ILogger) with a
null-logger. |
static void |
close(Closeable closeable,
ILogger logger)
This method can be used to simplify the typical finally
-block of code dealing with streams and readers/writers. |
static File |
commonRoot(Iterable<? extends File> files)
Determines the root directory from a collection of files. |
static int |
copy(InputStream input,
OutputStream output)
Copy an input stream to an output stream. |
static void |
copyFile(File sourceFile,
File targetFile)
Copy a file. |
static void |
copyFile(String sourceFilename,
String targetFilename)
Copy a file. |
static int |
copyFiles(File sourceDirectory,
File targetDirectory,
FileFilter fileFilter)
Copy all files specified by a file filter from one directory to another. |
static int |
createJARFile(File jarFile,
File directory,
FileFilter filter)
Create jar file from all files in a directory. |
static String |
createRelativePath(File path,
File relativeTo)
Returns a string describing the relative path to the given directory. |
static void |
deleteFile(File file)
Deletes the given file and throws an exception if this fails. |
static void |
deleteRecursively(File directory)
Recursively delete directories and files. |
static void |
ensureDirectoryExists(File file)
Checks if a directory exists. |
static void |
ensureParentDirectoryExists(File file)
Checks if the parent directory of a file exists. |
static File |
extractJarFileFromJarURL(URL url)
Returns the JAR file for an URL with protocol 'jar'. |
static String |
getFileExtension(File file)
Returns the extension of the file. |
static boolean |
isAbsolutePath(String filename)
Returns whether a filename represents an absolute path. |
static List<File> |
listFilesRecursively(File directory)
Returns a list of all files and directories contained in the given directory and all subdirectories. |
static List<File> |
listFilesRecursively(File directory,
FileFilter filter)
Returns a list of all files and directories contained in the given directory and all subdirectories matching the filter provided. |
static void |
mergeTemplate(File templateFile,
File outFile,
Object... arguments)
Loads template file with a Format string, formats it and writes result to file. |
static InputStream |
mergeTemplate(InputStream inStream,
Object... arguments)
Loads template file with a Format string, formats it and provides result as stream. |
static void |
mkdir(File dir)
Creates a directory and throws an exception if this fails. |
static void |
mkdirs(File dir)
Creates a directory and all required parent directories. |
static File |
newFile(File parent,
String... elements)
This method is similar to the constructor File.File(File, String)
but allows to define multiple child levels. |
static String |
normalizeSeparators(String path)
Replace platform dependent separator char with forward slashes to create system-independent paths. |
static String |
readFile(File file)
Read file content into a string using the default encoding for the platform. |
static String |
readFile(File file,
String encoding)
Read file content into a string using the given encoding. |
static String |
readFileUTF8(File file)
Read file content into a string using UTF-8 encoding. |
static Properties |
readPropertiesFile(File propertiesFile)
Reads properties from a properties file. |
static String |
readStream(InputStream input)
Read input stream into string. |
static String |
readStream(InputStream input,
String encoding)
Read input stream into string. |
static String |
readStreamUTF8(InputStream input)
Read input stream into string. |
static void |
renameFileTo(File file,
File dest)
Renames the given file and throws an exception if this fails. |
static void |
safeRead(InputStream in,
byte[] data)
Reads bytes of data from the input stream into an array of bytes until the array is full. |
static void |
safeRead(InputStream in,
byte[] data,
int offset,
int length)
Reads length bytes of data from the input stream into an
array of bytes and stores it at position offset. |
static void |
sort(List<File> files)
Compares files based on the lexical order of their fully qualified names. |
static void |
unjar(File jarFile,
File targetDirectory)
Extract a JAR file to a directory. |
static void |
writeFile(File file,
String content)
Write string to a file with the default encoding. |
static void |
writeFile(File file,
String content,
String encoding)
Write string to a file. |
static void |
writeFileUTF8(File file,
String content)
Write string to a file with UTF8 encoding. |
static void |
writeFileWithBOM(File file,
String content,
EByteOrderMark bom)
Write string to a file using a UTF encoding. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String UTF8_ENCODING
public static final Charset UTF8_CHARSET
| Constructor Detail |
|---|
public FileSystemUtils()
| Method Detail |
|---|
public static int copy(InputStream input,
OutputStream output)
throws IOException
input - input streamoutput - output stream
IOException - if an IO exception occurs.
public static void copyFile(File sourceFile,
File targetFile)
throws IOException
IOException
public static void copyFile(String sourceFilename,
String targetFilename)
throws IOException
IOException
public static int copyFiles(File sourceDirectory,
File targetDirectory,
FileFilter fileFilter)
throws IOException
sourceDirectory - source directorytargetDirectory - target directoryfileFilter - filter to specify file types. If all files should be copied,
use FileOnlyFilter.
IOException - if an exception occurs.
public static int createJARFile(File jarFile,
File directory,
FileFilter filter)
throws IOException
jarFile - jar file to create.directory - source directory.filter - filter to specify file types. If all files should be copied,
use FileOnlyFilter.
IOException - if an exception occurs.
public static String createRelativePath(File path,
File relativeTo)
throws IOException
path - the path to convert to a relative path (must describe an
existing directory)relativeTo - the anchor (must describe an existing directory)
IOException - if creation of canonical pathes fails.public static void deleteRecursively(File directory)
public static void deleteFile(File file)
throws IOException
IOExceptionFile.delete()
public static void renameFileTo(File file,
File dest)
throws IOException
IOExceptionFile.renameTo(File)
public static void mkdir(File dir)
throws IOException
IOExceptionFile.mkdir()
public static void mkdirs(File dir)
throws IOException
IOExceptionFile.mkdirs()
public static void ensureDirectoryExists(File file)
throws IOException
file - the directory
IOException - if directories couldn't be created.
public static void ensureParentDirectoryExists(File file)
throws IOException
file - the file
IOException - if directories couldn't be created.public static List<File> listFilesRecursively(File directory)
This method knows nothing about (symbolic and hard) links, so care should be taken when traversing directories containing recursive links.
directory - the directory to start the search from.
public static List<File> listFilesRecursively(File directory,
FileFilter filter)
The file filter may or may not exclude directories.
This method knows nothing about (symbolic and hard) links, so care should be taken when traversing directories containing recursive links.
directory - the directory to start the search from. If this is null or the
directory does not exists, an empty list is returned.filter - the filter used to determine whether the result should be
included. If the filter is null, all files and directories are
included.
public static String getFileExtension(File file)
null, if the file has no extension (i.e. if a
filename contains no '.'), returns the empty string if the '.' is
the filename's last character.
public static File newFile(File parent,
String... elements)
File.File(File, String)
but allows to define multiple child levels.
parent - parent fileelements - list of elements. If this is empty, the parent is returned.
public static String readFile(File file)
throws IOException
IOExceptionEByteOrderMark
public static String readFileUTF8(File file)
throws IOException
IOExceptionEByteOrderMark
public static String readFile(File file,
String encoding)
throws IOException,
UnsupportedEncodingException
IOException
UnsupportedEncodingExceptionEByteOrderMark
public static void unjar(File jarFile,
File targetDirectory)
throws IOException
jarFile - jar file to extracttargetDirectory - target directory.
IOException - if an exception occurs.
public static void writeFile(File file,
String content)
throws IOException
IOException
public static void writeFileUTF8(File file,
String content)
throws IOException
IOException
public static void writeFile(File file,
String content,
String encoding)
throws IOException
IOException
public static void writeFileWithBOM(File file,
String content,
EByteOrderMark bom)
throws IOException
IOException
public static void mergeTemplate(File templateFile,
File outFile,
Object... arguments)
throws IOException
templateFile - the template file with the format stringoutFile - the target file, parent directories are created automatically.arguments - the formatting arguments.
IOException - if an IO exception occurs or the template file defines an
illegal format.
public static InputStream mergeTemplate(InputStream inStream,
Object... arguments)
throws IOException
inStream - stream that provides the template format stringarguments - the formatting arguments.
IOException - if an IOException occurs or the template file defines an
illegal format.
public static String readStream(InputStream input)
throws IOException
IOException
public static String readStreamUTF8(InputStream input)
throws IOException
IOException
public static String readStream(InputStream input,
String encoding)
throws IOException
IOException
public static Properties readPropertiesFile(File propertiesFile)
throws IOException
IOException
public static File commonRoot(Iterable<? extends File> files)
throws IOException
This method does not require the input files to exist.
files - Collection of files for which root directory gets determined.
This collection is required to contain at least 2 files. If it
does not, an AssertionError is thrown.
PreconditionException - If less than two different files are provided whereas fully
qualified canonical names are used for comparison.
IOException - Since canonical paths are used for determination of the
common root, and File.getCanonicalPath() can throw
IOExceptions.@Deprecated public static String canonize(String path)
canonize(File)
@Deprecated public static File canonize(File file)
File.getCanonicalPath() and wraps IOExceptions thrown by
it into AssertionErrors, since we expect this method not to throw
IOExceptions.
public static InputStream autoDecompressStream(InputStream in)
throws IOException
GZIPInputStream
IOExceptionpublic static void close(Closeable closeable)
close(Closeable, ILogger) with a
null-logger.
public static void close(Closeable closeable,
ILogger logger)
finally
-block of code dealing with streams and readers/writers. It checks if the
provided closeable is null. If not it closes it. An
exception thrown during the close operation is logged with the provided
logger with level warn. If the provided logger is
null, no logging is performed. If no logging is required,
method close(Closeable) may also be used.
public static void sort(List<File> files)
public static String normalizeSeparators(String path)
public static File extractJarFileFromJarURL(URL url)
public static boolean isAbsolutePath(String filename)
File.isAbsolute() is operating system specific.
public static void safeRead(InputStream in,
byte[] data)
throws IOException,
EOFException
InputStream.read(byte[]) may
read less than the requested number of bytes, while this method ensures
the data is complete.
in - the stream to read from.data - the stream to read from.
IOException - if reading the underlying stream causes an exception.
EOFException - if the end of file was reached before the requested data was
read.
public static void safeRead(InputStream in,
byte[] data,
int offset,
int length)
throws IOException,
EOFException
length bytes of data from the input stream into an
array of bytes and stores it at position offset. This method
blocks until input data is available, end of file is detected, or an
exception is thrown.
The reason for this method is that
InputStream.read(byte[], int, int) may read less than the
requested number of bytes, while this method ensures the data is
complete.
in - the stream to read from.data - the stream to read from.offset - the offset in the array where the first read byte is stored.length - the length of data read.
IOException - if reading the underlying stream causes an exception.
EOFException - if the end of file was reached before the requested data was
read.
|
TUM CCSM Commons | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||