Package com.compomics.util.io
Class IoUtil
java.lang.Object
com.compomics.util.io.IoUtil
public class IoUtil extends Object
Utils for I/O and file handling.
- Author:
- Marc Vaudel, Harald Barsnes
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_SEPARATOR
Default separator for tabular files.static String
ENCODING
Default encoding, cf the second rule. -
Constructor Summary
Constructors Constructor Description IoUtil()
-
Method Summary
Modifier and Type Method Description static void
append(File in, File out)
Appends the content of a file to another.static String
appendSuffix(String fileName, String suffix)
Appends a suffix to a file name before the file extension.static boolean
checkIfURLExists(String targetUrlAsString, String userName, String password)
Check if a given URL exists.static void
closeBuffer(MappedByteBuffer buffer)
Attempts at closing a buffer.static void
copyFile(File in, File out)
Copy the content of a file to another.static void
copyFile(File in, File out, boolean overwrite)
Copy the content of one file to another.static boolean
deleteDir(File dir)
Deletes all files and subdirectories under dir and dir itself.static boolean
emptyDir(File dir)
Deletes all files and subdirectories under dir.static File
existsExtensionNotCaseSensitive(File file)
Checks if the given file exists with the extension in another case and returns it.static String
getExtension(File file)
Returns the extensions of a file.static String
getExtension(String fileName)
Returns the extensions of a file name.static String
getFileName(File file)
An OS independent getName alternative.static String
getFileName(String filePath)
An OS independent getName alternative.static String
getFilenameExtensionLowerCase(String fileName)
Returns the given file name with lower-case extension.static int
getFileSize(URL url)
Returns the size of the file located at the given URL.static String
removeExtension(String fileName)
Removes the extension from a file name or path.static File
saveUrl(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:
- Constant Field Values
-
DEFAULT_SEPARATOR
Default separator for tabular files.- See Also:
- Constant Field Values
-
-
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, FileNotFoundExceptionSave 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
-