Package uk.ac.gda.util.io
Class FileUtils
java.lang.Object
uk.ac.gda.util.io.FileUtils
A class with a collection of file management static utility classes. Several method contents copied from code
snippets available on the web.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final voidOverwrites destination_file if it exists, creates new if not.static final voidOverwrites destination_file if it exists, creates new if not.static final voidstatic final voidstatic final voidcopyNioNoCopyOver(File source_file, File destination_file) static FilecreateNewUniqueDir(File file) static FilecreateNewUniqueDir(File file, String templateName) static final voiddeleteContents(File parent) static StringformatSize(long longSize, int decimalPos) Formats a file sizestatic final byte[]getByteArrayFromStream(InputStream source) static final byte[]getByteArrayFromStream(InputStream source, boolean shouldClose) static final ByteArrayOutputStreamgetByteStream(InputStream source) static final ByteArrayOutputStreamgetByteStream(InputStream source, boolean shouldClose) static longgetDiskSpace(File dir) static StringgetFileExtension(File file) Get File extension (result will NOT include ".")static StringgetFileExtension(String fileName) Get File extension (result will NOT include ".")static StringgetFileNameNoExtension(File file) Get Filename minus it's extension if presentstatic longgetFileSizeRecursive(File file, boolean subFolders) static final StringgetParentDirName(String name) static StringgetSystemInfo(File file) Gets the last modified and size formatted as a stringstatic FileGenerates a unique file of the name template or template+an integerstatic Filestatic booleanReturns true if the string is starting with a BOMstatic booleanisDiskSpaceAvaliable(File tmp, long sizeInB) static booleanisDiskSpaceAvaliableMB(File tmp, long sizeInMB) static booleanThis method returns true if file is Unix and false if Windows line endings.static final StringBufferstatic final StringBufferreadFile(InputStream in) static final StringBufferreadFile(InputStream in, String charsetName) readFileAsList(File file, String encodingOfFile, boolean removeBom, boolean trimLines) Reads a file using the encoding parameter passed and returns each line as an item in the result list Optionally: the BOM can be removed (first character of file if present) the line can be trimmed of whitespacestatic final voidrecursiveCopy(File source_dir, File destination_dir) Recursively copy one folder to another Deleting the contents of the destination folder before copying.static final voidrecursiveCopyNio(File source_dir, File destination_dir) Recursively copy one folder to another Deleting the contents of the destination folder before copying.static final voidrecursiveCopyNioNoDelete(File source_dir, File destination_dir) Recursively copy one folder to another Not deleting the contents of the destination folder before copying.static final booleanrecursiveDelete(File parent) static final voidrecursiveDeleteOnExit(File parent) Recursively delete parent folder on exit of JVMstatic final voidrecursiveIncrementalCopy(File source_dir, File destination_dir) Recursively copy one folder to another, but not deleting destination data.static final voidrecursiveIncrementalCopy(File source_dir, File destination_dir, int[] inf) Recursively copy one folder to another, but not deleting destination data.static final voidwrite(BufferedInputStream source, File destination_file) static voidThis method attempts to write a string to file in US-ASCII.static voidstatic voidstatic final voidwrite(InputStream source_raw, OutputStream destination_raw) static voidwrite(OutputStream out, String text, String charset, boolean addBomIfNeeded) This method writes to a stream a potentially large String.static final voidwriteToFile(File file, InputStream fromNoClose)
-
Field Details
-
BOM
public static final char BOM
-
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
recursiveDelete
- Parameters:
parent-- Returns:
- boolean
-
deleteContents
- Parameters:
parent-
-
getUnique
Generates a unique file of the name template or template+an integer- Parameters:
dir-template-ext-- Returns:
- a unique file.
-
getUnique
- Parameters:
dir-template-ext-i-- Returns:
- file
-
recursiveDeleteOnExit
Recursively delete parent folder on exit of JVM- Parameters:
parent-
-
recursiveCopy
Recursively copy one folder to another Deleting the contents of the destination folder before copying. Use at your peril!- Parameters:
source_dir-destination_dir-- Throws:
IOException
-
recursiveCopyNio
Recursively copy one folder to another Deleting the contents of the destination folder before copying. Use at your peril!- Parameters:
source_dir-destination_dir-- Throws:
IOException
-
recursiveCopyNioNoDelete
public static final void recursiveCopyNioNoDelete(File source_dir, File destination_dir) throws IOException Recursively copy one folder to another Not deleting the contents of the destination folder before copying. Any file that already exists will not be copied.- Parameters:
source_dir-destination_dir-- Throws:
IOException
-
recursiveIncrementalCopy
public static final void recursiveIncrementalCopy(File source_dir, File destination_dir) throws IOException Recursively copy one folder to another, but not deleting destination data. Only copies files if timestamp indicates the data is newer. inf[0] - returns files copied, can be used to track progress inf[1] - returns files skipped, can be used to track progress inf[2] - if set to -1 will halt copy- Parameters:
source_dir-destination_dir-- Throws:
IOException
-
recursiveIncrementalCopy
public static final void recursiveIncrementalCopy(File source_dir, File destination_dir, int[] inf) throws IOException Recursively copy one folder to another, but not deleting destination data. Only copies files if timestamp indicates the data is newer. inf[0] - returns files copied, can be used to track progress inf[1] - returns files skipped, can be used to track progress inf[2] - if set to -1 will halt copy- Parameters:
source_dir-destination_dir-inf-- Throws:
IOException
-
copy
- Parameters:
source_file-destination_dir-- Throws:
IOException
-
copy
Overwrites destination_file if it exists, creates new if not.- Parameters:
source_file-destination_file-- Throws:
IOException
-
copy
public static final void copy(File source_file, File destination_file, byte[] buffer) throws IOException Overwrites destination_file if it exists, creates new if not.- Parameters:
source_file-destination_file-buffer-- Throws:
IOException
-
copyNio
- Parameters:
source_file-destination_file-- Throws:
IOException
-
copyNioNoCopyOver
public static final void copyNioNoCopyOver(File source_file, File destination_file) throws IOException - Parameters:
source_file-destination_file-- Throws:
IOException
-
write
public static final void write(BufferedInputStream source, File destination_file) throws IOException - Parameters:
source-destination_file-- Throws:
IOException
-
write
public static final void write(InputStream source_raw, OutputStream destination_raw) throws IOException - Parameters:
source_raw-destination_raw-- Throws:
IOException
-
getByteArrayFromStream
- Parameters:
source-- Returns:
- byte[]
- Throws:
IOException
-
getByteArrayFromStream
public static final byte[] getByteArrayFromStream(InputStream source, boolean shouldClose) throws IOException - Parameters:
source-shouldClose-- Returns:
- byte[]
- Throws:
IOException
-
getByteStream
- Parameters:
source-- Returns:
- ByteArrayOutputStream
- Throws:
IOException
-
getByteStream
public static final ByteArrayOutputStream getByteStream(InputStream source, boolean shouldClose) throws IOException - Parameters:
source-shouldClose-- Returns:
- ByteArrayOutputStream
- Throws:
IOException
-
readFile
- Parameters:
file-- Returns:
- StringBuffer
- Throws:
Exception
-
readFile
- Parameters:
in-- Returns:
- StringBuffer
- Throws:
Exception
-
readFile
- Parameters:
in-charsetName-- Returns:
- StringBuffer
- Throws:
Exception
-
readFileAsList
- Parameters:
f-- Returns:
- List
- Throws:
Exception
-
readFileAsList
public static final List<String> readFileAsList(File file, String encodingOfFile, boolean removeBom, boolean trimLines) throws IOException Reads a file using the encoding parameter passed and returns each line as an item in the result list Optionally: the BOM can be removed (first character of file if present) the line can be trimmed of whitespace- Parameters:
file- The file to readencodingOfFile- The encoding format the file should be read asremoveBom- Whether the BOM should be removed, if present.trimLines- If each line should have leading and trailing whitespace removed- Returns:
- List
lines of the file optionally trimmed - Throws:
IOException- If the file can not be read
-
isUnix
This method returns true if file is Unix and false if Windows line endings.- Parameters:
stringToTest- The text of the file- Returns:
- boolean true if the file is Unix.
-
isBOM
Returns true if the string is starting with a BOM- Parameters:
stringToTest-- Returns:
- boolean
-
write
- Parameters:
file-text-encoding-- Throws:
Exception
-
write
This method attempts to write a string to file in US-ASCII. The code was moved directly from atos.SaveTextFile and no check has been made to how efficient it is.- Parameters:
file-text-- Throws:
Exception
-
write
public static void write(OutputStream out, String text, String charset, boolean addBomIfNeeded) throws Exception This method writes to a stream a potentially large String. The current limit for Java servlets in 90Mb with this method there is a workaround in Java 1.5 for this problem: http://forum.java.sun.com/thread.jspa?threadID=418441invalid input: '&messageID'=2816084 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026745- Parameters:
out-text-charset-addBomIfNeeded-- Throws:
Exception
-
write
- Parameters:
file-list-- Throws:
Exception
-
createNewUniqueDir
- Parameters:
file-- Returns:
- File
-
createNewUniqueDir
- Parameters:
file-templateName-- Returns:
- File
-
getParentDirName
- Parameters:
name-- Returns:
- String
-
writeToFile
- Parameters:
file-fromNoClose-- Throws:
Exception
-
getDiskSpace
- Parameters:
dir-- Returns:
- long
-
isDiskSpaceAvaliableMB
- Parameters:
tmp-sizeInMB-- Returns:
- boolean
- Throws:
IOException
-
isDiskSpaceAvaliable
- Parameters:
tmp-sizeInB-- Returns:
- boolean
- Throws:
IOException
-
getFileSizeRecursive
- Parameters:
file-subFolders-- Returns:
- long
-
getFileExtension
Get File extension (result will NOT include ".")- Parameters:
fileName-- Returns:
- String file extension value, or "" is no extension
-
getFileExtension
Get File extension (result will NOT include ".")- Parameters:
file-- Returns:
- String file extension value, or "" is no extension
-
getFileNameNoExtension
Get Filename minus it's extension if present- Parameters:
file- File to get filename from- Returns:
- String filename minus its extension
-
formatSize
Formats a file size- Parameters:
longSize-decimalPos-- Returns:
- formatted string for size.
-
getSystemInfo
Gets the last modified and size formatted as a string- Parameters:
file-- Returns:
- date and size
-