Package nz.ac.waikato.cms.core
Class FileUtils
java.lang.Object
nz.ac.waikato.cms.core.FileUtils
Utility class for I/O related actions.
- Version:
- $Revision: 10824 $
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCloses the stream, if possible, suppressing any exception.static voidCloses the stream, if possible, suppressing any exception.static voidcloseQuietly(Reader reader) Closes the reader, if possible, suppressing any exception.static voidcloseQuietly(Writer writer) Closes the writer, if possible, suppressing any exception.static booleancopyOrMove(File sourceLocation, File targetLocation, boolean move, boolean atomic) Copies or moves files and directories (recursively).static booleanDeletes the specified file.static StringgetExtension(File file) Returns the extension of the file, if any.static StringgetExtension(String filename) Returns the extension of the file, if any.static String[]getExtensions(File file) Returns the extensions of the file, if any.static String[]getExtensions(String filename) Returns the extensions of the file, if any.static StringremoveIgnoredExtensionSuffixes(String filename) Removes any ignored extension suffixes from the filename.static FilereplaceExtension(File file, String newExt) Replaces the extension of the given file with the new one.static StringreplaceExtension(String file, String newExt) Replaces the extension of the given file with the new one.static StringwriteToFileMsg(String filename, Object obj, boolean append, String encoding) Writes the given object to the specified file.
-
Field Details
-
MAX_EXTENSION_LENGTH
the maximum length for an extension. -
IGNORED_EXTENSION_SUFFIXES
the ignored extension suffixes.
-
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
replaceExtension
Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.- Parameters:
file- the file to replace the extension fornewExt- the new extension (incl dot), empty string to remove extension- Returns:
- the updated file
-
replaceExtension
Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.- Parameters:
file- the file to replace the extension fornewExt- the new extension (incl dot), empty string to remove extension- Returns:
- the updated file
-
getExtension
Returns the extension of the file, if any.- Parameters:
file- the file to get the extension from- Returns:
- the extension (no dot), null if none available
-
getExtension
Returns the extension of the file, if any.- Parameters:
filename- the file to get the extension from- Returns:
- the extension (no dot), null if none available
-
getExtensions
Returns the extensions of the file, if any. Returns "txt.gz" and "gz", for instance, for file "hello_world.txt.gz". The longer extension always comes first.- Parameters:
file- the file to get the extensions from- Returns:
- the extensions (no dot), null if none available
-
getExtensions
Returns the extensions of the file, if any. Removes ignored file extension suffixes like "gz" first.- Parameters:
filename- the file to get the extensions from- Returns:
- the extensions (no dot), null if none available
-
removeIgnoredExtensionSuffixes
Removes any ignored extension suffixes from the filename.- Parameters:
filename- the filename to process- Returns:
- the processed filename
-
closeQuietly
Closes the stream, if possible, suppressing any exception.- Parameters:
is- the stream to close
-
closeQuietly
Closes the stream, if possible, suppressing any exception.- Parameters:
os- the stream to close
-
closeQuietly
Closes the reader, if possible, suppressing any exception.- Parameters:
reader- the reader to close
-
closeQuietly
Closes the writer, if possible, suppressing any exception.- Parameters:
writer- the writer to close
-
delete
Deletes the specified file. If the file represents a directory, then this will get deleted recursively.- Parameters:
file- the file/dir to delete- Returns:
- true if successfully deleted
-
copyOrMove
public static boolean copyOrMove(File sourceLocation, File targetLocation, boolean move, boolean atomic) throws IOException Copies or moves files and directories (recursively). If targetLocation does not exist, it will be created.
Original code from Java-Tips.org.- Parameters:
sourceLocation- the source file/dirtargetLocation- the target file/dirmove- if true then the source files/dirs get deleted as soon as copying finishedatomic- whether to perform an atomic move operation- Returns:
- false if failed to delete when moving or failed to create target directory
- Throws:
IOException- if copying/moving fails
-
writeToFileMsg
Writes the given object to the specified file. The message is either appended or replaces the current content of the file.- Parameters:
filename- the file to write toobj- the object to writeappend- whether to append the message or notencoding- the encoding to use, null for default- Returns:
- true if writing was successful
-