Package com.compomics.util.io
Class IoUtil
java.lang.Object
com.compomics.util.io.IoUtil
Utils for I/O and file handling.
- Author:
- Marc Vaudel, Harald Barsnes
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAppends the content of a file to another.static StringappendSuffix(String fileName, String suffix) Appends a suffix to a file name before the file extension.static booleancheckIfURLExists(String targetUrlAsString, String userName, String password) Check if a given URL exists.static voidcloseBuffer(MappedByteBuffer buffer) Attempts at closing a buffer.static voidCopy the content of a file to another.static voidCopy the content of one file to another.static booleanDeletes all files and subdirectories under dir and dir itself.static booleanDeletes all files and subdirectories under dir.static FileChecks if the given file exists with the extension in another case and returns it.static StringgetExtension(File file) Returns the extensions of a file.static StringgetExtension(String fileName) Returns the extensions of a file name.static StringgetFileName(File file) An OS independent getName alternative.static StringgetFileName(String filePath) An OS independent getName alternative.static StringgetFilenameExtensionLowerCase(String fileName) Returns the given file name with lower-case extension.static intgetFileSize(URL url) Returns the size of the file located at the given URL.static StringremoveExtension(String fileName) Removes the extension from a file name or path.static FilesaveUrl(File saveFile, String targetUrlAsString, int fileSizeInBytes, String userName, String password, WaitingHandler waitingHandler) Save a file from a URL.
-
Field Details
-
ENCODING
Default encoding, cf the second rule.- See Also:
-
DEFAULT_SEPARATOR
Default separator for tabular files.- See Also:
-
-
Constructor Details
-
IoUtil
public IoUtil()
-
-
Method Details
-
emptyDir
Deletes all files and subdirectories under dir. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.- Parameters:
dir- the directory to delete- Returns:
- rue if all deletions were successful
-
deleteDir
Deletes all files and subdirectories under dir and dir itself. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.- Parameters:
dir- the directory to delete- Returns:
- rue if all deletions were successful
-
copyFile
Copy the content of a file to another.- Parameters:
in- the file to copy fromout- the file to copy to- Throws:
IOException- if a problem occurs when writing to the file
-
append
Appends the content of a file to another.- Parameters:
in- the file to copy fromout- the file to copy to- Throws:
IOException- if a problem occurs when writing to the file
-
copyFile
Copy the content of one file to another.- Parameters:
in- the file to copy fromout- the file to copy tooverwrite- boolean indicating whether out should be overwritten- Throws:
IOException- if an error occurred while reading or writing a file
-
getFileName
An OS independent getName alternative. Useful if the path is provided as a hardcoded string and opened in a different OS.- Parameters:
filePath- the file path as a string- Returns:
- the file name, or the complete path of no file name is detected
-
getFileName
An OS independent getName alternative. Useful if the path is provided as a hardcoded string and opened in a different OS.- Parameters:
file- the file- Returns:
- the file name, or the complete path of no file name is detected
-
getExtension
Returns the extensions of a file.- Parameters:
file- the file- Returns:
- the extension of a file
-
getExtension
Returns the extensions of a file name.- Parameters:
fileName- The file name.- Returns:
- The extension of the file name.
-
getFilenameExtensionLowerCase
Returns the given file name with lower-case extension.- Parameters:
fileName- The name of the file.- Returns:
- The name of the file with lower-case extension.
-
existsExtensionNotCaseSensitive
Checks if the given file exists with the extension in another case and returns it. Returns the given file otherwise.- Parameters:
file- The file to check.- Returns:
- The existing file with another extension if it exists, the given file otherwise.
-
appendSuffix
Appends a suffix to a file name before the file extension.- Parameters:
fileName- the file namesuffix- the suffix to add- Returns:
- the file name with suffix
-
removeExtension
Removes the extension from a file name or path.- Parameters:
fileName- the file name- Returns:
- the file name without extension
-
saveUrl
public static File saveUrl(File saveFile, String targetUrlAsString, int fileSizeInBytes, String userName, String password, WaitingHandler waitingHandler) throws MalformedURLException, IOException, FileNotFoundException Save a file from a URL.- Parameters:
saveFile- the file to save totargetUrlAsString- the target URL as a stringfileSizeInBytes- the file size in bytesuserName- the user namepassword- the passwordwaitingHandler- the waiting handler- Returns:
- the saved file
- Throws:
MalformedURLException- thrown if an MalformedURLException occursIOException- thrown if an IOException occursFileNotFoundException- thrown if a FileNotFoundException occurs
-
checkIfURLExists
Check if a given URL exists.- Parameters:
targetUrlAsString- the URL to checkuserName- the user namepassword- the password- Returns:
- true of it exists
-
getFileSize
Returns the size of the file located at the given URL.- Parameters:
url- the url of the file- Returns:
- the size of the file
-
closeBuffer
Attempts at closing a buffer. Taken from https://stackoverflow.com/questions/2972986/how-to-unmap-a-file-from-memory-mapped-using-filechannel-in-java.- Parameters:
buffer- the buffer to close
-