Class FileUtilities
- java.lang.Object
-
- com.biomatters.geneious.publicapi.utilities.FileUtilities
-
public class FileUtilities extends java.lang.Object
Provides utility methods for dealing with files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileUtilities.RetryPolicy
Specifies a policy about what to do if a file action (e.g.static class
FileUtilities.TargetExistsAction
The action to take when moving/copying files and the target already exists
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_FILENAME_LENGTH
Maximum allowed filename (just name, excluding path) length on the operating systems supported by Geneious.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
copyDirectory(java.io.File sourceDirectory, java.io.File destinationDirectory, boolean overwrite, jebl.util.ProgressListener progressListener)
Recursively copies the entire contents of one directory to another.static void
copyFile(java.io.File sourceFile, java.io.File destinationFile, FileUtilities.TargetExistsAction targetExistsAction, jebl.util.ProgressListener progressListener)
Copies the contents of a file to another file If canceled in the middle of the copy (so that the destination file is partially written), the destination file will be deleted.static boolean
createHardLink(java.io.File existingFile, java.io.File newFile)
Creates a file system hard link.static java.io.File
createTempDir(boolean deleteDirectoryOnceItIsNoLongerReferenced)
Creates a temporary directory.static java.io.File
createTempFile(java.lang.String fileName, boolean deleteFileAndParentFolderWhenFileIsNoLongerReferenced)
Returns a temporary file with the given name.static java.io.File
createTempFile(java.lang.String prefix, java.lang.String suffix)
Deprecated.usecreateTempFile(String, String, boolean)
instead.static java.io.File
createTempFile(java.lang.String prefix, java.lang.String suffix, boolean deleteFileOnceItIsNoLongerReferenced)
Creates a temporary file as specified byFile.createTempFile(String, String, java.io.File)
.static boolean
deleteDirectory(java.io.File path, jebl.util.ProgressListener progressListener)
Recursively deletes a directory.static boolean
deleteFileOrDirectory(java.io.File file, boolean retryIfNecessary)
Deletes the given file or directory.static boolean
deleteFileOrDirectory(java.io.File file, FileUtilities.RetryPolicy retryPolicy)
Deletes the given file or directory.static java.lang.String
getCanonicalOrAbsolutePath(java.io.File file)
Returns the result of callingFile.getCanonicalPath()
, or if that fails, returns the result of callingFile.getAbsolutePath()
.static java.lang.String
getFileNameExtension(java.io.File file)
Get the extension of a file name.static java.lang.String
getFileNameWithoutExtension(java.io.File file)
Get the name of a file, excluding its path, excluding its extension and excluding the '.' prior to its extension.static long
getFileOrFolderSize(java.io.File file, jebl.util.ProgressListener progressListener)
If file is not a directory this returnsFile.length()
, if file is a directory this recurses through all files in the directory and sub directories to return the sum ofFile.length()
s.static java.lang.String
getLegalFileName(java.lang.String fileName, java.lang.String illegalCharacterReplacement)
Get a file name that is guaranteed to be legal by replacing all characters in the given string that are not allowed in a file name with the given replacement.static java.io.File
getResourceForClass(java.lang.Class cl, java.lang.String resourceName)
for grabbing resources based on a directory hirarchy identical to the one that the classcl
is found in.static java.lang.String
getTextFromFile(java.io.File file)
Reads the entire contents of a file into a string.static java.lang.String
getTextFromFile(java.io.File file, int maxCharactersToRead)
Reads the contents of a file into a string, but at most maxCharactersToRead.static java.io.File
getUserSelectedFile(java.lang.String title, java.io.FilenameFilter filter, int fileSelectionType)
Displays a file open dialog to the user (a Java dialog on windows and unix, a Mac dialog on MacOS), and returns the file that the user selected (or null if they cancelled)static java.io.File
getUserSelectedSaveFile(java.lang.String approveButtonText, java.lang.String title, java.lang.String defaultFileName, java.lang.String defaultFilterExtension)
Displays a file save dialog to the user (a Java dialog on windows and unix, a Mac dialog on MacOS), and returns the file that the user selected (or null if they cancelled)static java.lang.String
isFileNameAllowed(java.lang.String fileName)
Will check a filename for validity on all platforms supported by Geneious.static java.io.FileInputStream
newFileInputStreamWithRetry(java.io.File file)
Attempts to create a new FileInputStream, but if it fails, retries a few times until it succeeds.static java.io.FileOutputStream
newFileOutputStreamWithRetry(java.io.File file)
Attempts to create a new FileOutputStream, but if it fails with a FileNotFoundException, retries a few times until it succeeds.static java.io.RandomAccessFile
newRandomAccessFileWithRetry(java.io.File file, java.lang.String mode)
Attempts to create a new RandomAccessFile, but if it fails, retries a few times until it succeeds.static boolean
renameToWithRetry(java.io.File source, java.io.File destination)
Similar toFile.renameTo(java.io.File)
except that if the rename fails, we retry a few times (performing garbage collections and sleeping briefly between attempts) if necessary until it succeeds, potentially waiting for up to approximately 1 second.static void
setExecutableFlags(java.io.File file)
Set the executable flag on a file, or do nothing if the OS does not support this behaviour Since Geneious 5.1.2 will accept a directory as argument to world execute on the entire directory tree A symlink pointing to a file that is not already executable will cause an IOException to be thrown All other symlinks are ignored after attempting to resolve themstatic void
unzip(java.io.File zipFile, java.io.File destinationDirectory, jebl.util.ProgressListener progressListener)
Extract a zip file.static void
writeTextToFile(java.io.File file, java.lang.String text)
Writes the given text to a file, overwriting any existing contents of the file.static void
zip(java.io.File fileOrDirectory, java.io.File zipFile, boolean includeRootDirectory, jebl.util.ProgressListener progressListener)
Create a compressed zip containing a file or directory.static void
zipWithoutCompression(java.io.File fileOrDirectory, java.io.File zipFile, boolean includeRootDirectory, jebl.util.ProgressListener progressListener)
Create a zip containing a file or directory without any compression.
-
-
-
Field Detail
-
MAX_FILENAME_LENGTH
public static final int MAX_FILENAME_LENGTH
Maximum allowed filename (just name, excluding path) length on the operating systems supported by Geneious.- Since:
- API 4.202010 (Geneious Prime 2020.1.0)
- See Also:
- Constant Field Values
-
-
Method Detail
-
isFileNameAllowed
public static java.lang.String isFileNameAllowed(java.lang.String fileName)
Will check a filename for validity on all platforms supported by Geneious. eg. checks for illegal filename characters and length.- Parameters:
fileName
- the name of a file excluding the path. must not be null- Returns:
- null if filename is valid or a message describing why it is invalid if it is not valid.
-
getLegalFileName
public static java.lang.String getLegalFileName(java.lang.String fileName, java.lang.String illegalCharacterReplacement)
Get a file name that is guaranteed to be legal by replacing all characters in the given string that are not allowed in a file name with the given replacement. If the name has an illegal extension or if the name itself is illegal then '1' will be appended to the name to make it legal.- Parameters:
fileName
- file name to make legalillegalCharacterReplacement
- string to replace every occurrence of an illegal character with- Returns:
- a guaranteed legal version of the file name or the unmodified
fileName
if the name was already legal.
-
getFileNameWithoutExtension
public static java.lang.String getFileNameWithoutExtension(java.io.File file)
Get the name of a file, excluding its path, excluding its extension and excluding the '.' prior to its extension. If the file has no extension, the file name (excluding its path) is returned.- Parameters:
file
- the file to get the name for- Returns:
- the name of a file, excluding its path, excluding its extension and excluding the '.' prior to its extension.
-
getFileNameExtension
public static java.lang.String getFileNameExtension(java.io.File file)
Get the extension of a file name. This is the suffix of the file name after the last '.'- Parameters:
file
- the file to get the extension for- Returns:
- the extension of the file name excluding the '.'. If the file has no extension returns an empty string.
-
getTextFromFile
public static java.lang.String getTextFromFile(java.io.File file) throws java.io.IOException
Reads the entire contents of a file into a string. Line endings will be imported as they are in the file (platform dependent) so you may get\r\n
which can cause problems.- Parameters:
file
- the file to load- Returns:
- the contents of the file
- Throws:
java.io.IOException
- if any method calls to the File throw an IOException- See Also:
writeTextToFile(java.io.File, String)
,getTextFromFile(File, int)
,StringUtilities.getTextFromInputStream(java.io.InputStream,boolean)
,StringUtilities.getTextFromReader(java.io.Reader, boolean)
-
getTextFromFile
public static java.lang.String getTextFromFile(java.io.File file, int maxCharactersToRead) throws java.io.IOException
Reads the contents of a file into a string, but at most maxCharactersToRead. Line endings will be imported as they are in the file (platform dependent) so you may get\r\n
which can cause problems.- Parameters:
file
- the file to loadmaxCharactersToRead
- the maximum number of characters to read from the file- Returns:
- the contents of the file
- Throws:
java.io.IOException
- if any method calls to the File throw an IOException- Since:
- API 4.1100 (Geneious 11.0.0)
- See Also:
getTextFromFile(File)
,writeTextToFile(java.io.File, String)
,StringUtilities.getTextFromInputStream(java.io.InputStream,boolean)
,StringUtilities.getTextFromReader(java.io.Reader, boolean)
-
writeTextToFile
public static void writeTextToFile(java.io.File file, java.lang.String text) throws java.io.IOException
Writes the given text to a file, overwriting any existing contents of the file.- Parameters:
file
- the file to write to.text
- the text to write to the file- Throws:
java.io.IOException
- if any method calls to the File throw an IOException- See Also:
getTextFromFile(java.io.File)
-
copyFile
public static void copyFile(java.io.File sourceFile, java.io.File destinationFile, FileUtilities.TargetExistsAction targetExistsAction, jebl.util.ProgressListener progressListener) throws java.io.IOException
Copies the contents of a file to another file If canceled in the middle of the copy (so that the destination file is partially written), the destination file will be deleted. Note that in this case, a previously existing destinationFile will be erased if targetExistsAction == Overwrite.- Parameters:
sourceFile
- the file to copy the contents fromdestinationFile
- the file to copy the contents totargetExistsAction
- action to take when the destination file already exists: Fail (throw an IOException), skip (silently keep the destination file), overwrite (silently copy anyway, overwriting the destination file)progressListener
- a progress listener to notify about progress during the copy and also provide the ability to cancel the copy. If the copy is cancelled, any partially written destination file will be removed. Must not be null, but may beProgressListener.EMPTY
.- Throws:
java.io.IOException
- if the file cannot be copied for any reason.
-
copyDirectory
public static void copyDirectory(java.io.File sourceDirectory, java.io.File destinationDirectory, boolean overwrite, jebl.util.ProgressListener progressListener) throws java.io.IOException
Recursively copies the entire contents of one directory to another. Note: ignores .cvs and .svn directories. If canceled, this method does not restore the destination to its original state.- Parameters:
sourceDirectory
- the directory to copy the contents ofdestinationDirectory
- the directly to copy the contents tooverwrite
- true to overwrite files of the same name that exist in the desitination directory.progressListener
- ProgressListener to report progress of the copying to. If the progressListener is canceled, this method does not restore the destination to its original state. Must not be null, but may beProgressListener.EMPTY
.- Throws:
java.io.IOException
- if the directory cannot be copied for any reason.java.lang.IllegalArgumentException
- ifsourceDirectory
is not a directory.
-
getFileOrFolderSize
public static long getFileOrFolderSize(java.io.File file, jebl.util.ProgressListener progressListener)
If file is not a directory this returnsFile.length()
, if file is a directory this recurses through all files in the directory and sub directories to return the sum ofFile.length()
s.- Parameters:
file
- a directory or file to calculate the size ofprogressListener
- since this method may potentially take a long time, this may be used to abort this method in which case it returns the total size calculated so far. This method does not report any progress (rather it only reports to the progressListeners message the size calculated so far and uses it for determining whether to cancel). progressListener may be null.- Returns:
- total size of file or all contained files for a directory.
-
zip
public static void zip(java.io.File fileOrDirectory, java.io.File zipFile, boolean includeRootDirectory, jebl.util.ProgressListener progressListener) throws java.io.IOException
Create a compressed zip containing a file or directory.- Parameters:
fileOrDirectory
- a file or directory to be compressedzipFile
- the target zip file. will be created if doesn't exist, will be replaced if already existsincludeRootDirectory
- true to includefileOrDirectory
in the zip file, false to only include the contents offileOrDirectory
. Only applicable when a directory is passed in, ignored otherwise.progressListener
- allows canceling and progress reporting- Throws:
java.io.IOException
- if the zip file couldn't be written for any reason- Since:
- API 4.14 (Geneious 5.1)
-
zipWithoutCompression
public static void zipWithoutCompression(java.io.File fileOrDirectory, java.io.File zipFile, boolean includeRootDirectory, jebl.util.ProgressListener progressListener) throws java.io.IOException
Create a zip containing a file or directory without any compression. Same aszip(java.io.File, java.io.File, boolean, jebl.util.ProgressListener)
except much faster and zip file will be larger.- Parameters:
fileOrDirectory
- a file or directory to be compressedzipFile
- the target zip file. will be created if doesn't exist, will be replaced if already existsincludeRootDirectory
- true to includefileOrDirectory
in the zip file, false to only include the contents offileOrDirectory
. Only applicable when a directory is passed in, ignored otherwise.progressListener
- allows canceling and progress reporting- Throws:
java.io.IOException
- if the zip file couldn't be written for any reason- Since:
- API 4.51 (Geneious 5.5.1)
-
unzip
public static void unzip(java.io.File zipFile, java.io.File destinationDirectory, jebl.util.ProgressListener progressListener) throws java.io.IOException
Extract a zip file.- Parameters:
zipFile
- the zip file to be extracteddestinationDirectory
- the directory to extract the contents of the zip file toprogressListener
- allows canceling and progress reporting- Throws:
java.io.IOException
- if extracted files couldn't be written for any reason- Since:
- API 4.14 (Geneious 5.1)
-
deleteDirectory
public static boolean deleteDirectory(java.io.File path, jebl.util.ProgressListener progressListener)
Recursively deletes a directory. UnlikeFile.delete()
this method will remove non-empty directories.- Parameters:
path
- the directory to deleteprogressListener
- ProgressListener to report progress of the deletion to. If the progressListener is canceled, this method does not restore any already deleted files or sub-directories. Must not be null, but may beProgressListener.EMPTY
.- Returns:
- true if and only if the directory was successfully deleted. In keeping with the
File.delete()
contract, this means this method will return false if the directory did not previously exist.
-
renameToWithRetry
public static boolean renameToWithRetry(java.io.File source, java.io.File destination)
Similar toFile.renameTo(java.io.File)
except that if the rename fails, we retry a few times (performing garbage collections and sleeping briefly between attempts) if necessary until it succeeds, potentially waiting for up to approximately 1 second. This is a workaround for Java bug 6213298 at http://bugs.sun.com/view_bug.do?bug_id=6213298- Parameters:
source
- the file or directory to renamedestination
- the new name for the source file.- Returns:
true
if and only if the renaming succeeded;false
otherwise (just likeFile.renameTo(java.io.File)
)
-
setExecutableFlags
public static void setExecutableFlags(java.io.File file) throws java.io.IOException, java.lang.InterruptedException
Set the executable flag on a file, or do nothing if the OS does not support this behaviour Since Geneious 5.1.2 will accept a directory as argument to world execute on the entire directory tree A symlink pointing to a file that is not already executable will cause an IOException to be thrown All other symlinks are ignored after attempting to resolve them- Parameters:
file
- file or directory to set permissions for- Throws:
java.lang.InterruptedException
- if something interrupts the process setting the permissionsjava.io.IOException
- if something else goes wrong
-
getCanonicalOrAbsolutePath
public static java.lang.String getCanonicalOrAbsolutePath(java.io.File file)
Returns the result of callingFile.getCanonicalPath()
, or if that fails, returns the result of callingFile.getAbsolutePath()
.- Parameters:
file
- the file to get the canonical or absolute path of.- Returns:
- the result of calling
File.getCanonicalPath()
, or if that fails, returns the result of callingFile.getAbsolutePath()
.
-
deleteFileOrDirectory
public static boolean deleteFileOrDirectory(java.io.File file, boolean retryIfNecessary)
Deletes the given file or directory.- Parameters:
file
- the file or directory to delete.retryIfNecessary
- true to retry a few times for up to approximately a second to delete the file if it can't be deleted the first time, performing garbage collection and sleeping briefly between attempts- Returns:
- true if the file exists and was deleted. true if the file did not exist. false if the file or directory exists and we cannot delete it.
-
deleteFileOrDirectory
public static boolean deleteFileOrDirectory(java.io.File file, FileUtilities.RetryPolicy retryPolicy)
Deletes the given file or directory.- Parameters:
file
- the file or directory to delete.retryPolicy
- if the file can't be deleted, indicates what to do. If it is RetryPolicy.Never returns false. If it is RetryPolicy.Now a few attempts are immediately made to delete the file. If it is RetryPolicy.Later, this method returns false, the file will be queued, and it will attempts to delete it will be made periodically until successful.- Returns:
- true if the file exists and was deleted. true if the file did not exist. false if the file or directory exists and we cannot delete it.
- Since:
- API 4.40 (Geneious 5.4.0)
-
getUserSelectedFile
public static java.io.File getUserSelectedFile(java.lang.String title, java.io.FilenameFilter filter, int fileSelectionType)
Displays a file open dialog to the user (a Java dialog on windows and unix, a Mac dialog on MacOS), and returns the file that the user selected (or null if they cancelled)- Parameters:
title
- the title of the file dialogfilter
- to filter the files displayed in the dialogfileSelectionType
- eitherJFileChooser.FILES_AND_DIRECTORIES
,JFileChooser.FILES_ONLY
, orJFileChooser.DIRECTORIES_ONLY
- Returns:
- the file that the user selected (or null if they cancelled)
-
getUserSelectedSaveFile
public static java.io.File getUserSelectedSaveFile(java.lang.String approveButtonText, java.lang.String title, java.lang.String defaultFileName, java.lang.String defaultFilterExtension)
Displays a file save dialog to the user (a Java dialog on windows and unix, a Mac dialog on MacOS), and returns the file that the user selected (or null if they cancelled)- Parameters:
title
- the title of the file dialogdefaultFileName
- the default name for the file (this will be entered into the file dialog when it first opens)defaultFilterExtension
- the default extension for the file filter- Returns:
- the file that the user selected (or null if they cancelled)
- Since:
- API 4.14 (Geneious 5.1)
-
createTempFile
@Deprecated public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
Deprecated.usecreateTempFile(String, String, boolean)
instead.Equivalent tocreateTempFile(prefix, suffix, true)
- Throws:
java.io.IOException
-
createTempFile
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, boolean deleteFileOnceItIsNoLongerReferenced) throws java.io.IOException
Creates a temporary file as specified byFile.createTempFile(String, String, java.io.File)
. If Geneious is running when this method is invoked, Geneious will supply a temporary directory in the user's data directory which will have its contents deleted when Geneious is next started. If Geneious is not running when this method is invoked, the standard system temporary directory is used. It is preferable to use this method rather thanFile.createTempFile(String, String, java.io.File)
because- If the machine or Geneious terminates before the code deletes the temporary file, with File.createTempFile the temporary file may never get deleted. With Geneious the temporary file will be deleted on the next start up of Geneious.
- We have had reports of very slow performance when Geneious uses File.createTempFile. This occurs when the user has 100,000+ files in their system temp folder.
- File.createTempFile may create files readable by any user in the system temp folder which is not desirable for sensitive data.
File.getPath()
orFile.toString()
are no longer referenced in this JVM. The returned file will already have had its path canonicalized (viaFile.getCanonicalPath()
) so there is no need to call that method on the returned file and doing so will not hold a reference to the given file potentially causing it to be deleted early. No matter what the value of deleteFileOnceItIsNoLongerReferenced, the file will be deleted if it still exists when the user next starts Geneious. Do not set this to true for files passed to external command line programs or the file may end up getting deleted before the external command line program accesses it. The user may change their data directory from preferences while Geneious is running, so plugins should not assume the path of the returned file to necessarily be the same from separate calls to this method.- Parameters:
prefix
- - The prefix string to be used in generating the file's name; must be at least three characters longsuffix
- - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be useddeleteFileOnceItIsNoLongerReferenced
- - see above. use false to make this method behave likeFile.createTempFile(String, String, java.io.File)
. WARNING: do not set this to true for files passed to external command line programs or the file may end up getting deleted before the external command line program accesses it.- Returns:
- a file that will be automatically deleted when the given file object (or it's path) are no longer referenced.
- Throws:
java.io.IOException
- If a file could not be created- Since:
- API 4.14 (Geneious 5.1)
-
createTempFile
public static java.io.File createTempFile(java.lang.String fileName, boolean deleteFileAndParentFolderWhenFileIsNoLongerReferenced) throws java.io.IOException
Returns a temporary file with the given name. This file will be created in a newly created sub-folder of the temp folder and the file is guaranteed to have the given name unlikecreateTempFile(String, String, boolean)
which only allows specifying the prefix or suffix- Parameters:
fileName
- the name of the file to returndeleteFileAndParentFolderWhenFileIsNoLongerReferenced
- true to delete the parent sub-folder and everything in it once this file is no longer referenced.- Returns:
- a temporary file
- Throws:
java.io.IOException
- If a file or directory could not be created- Since:
- API 4.700 (Geneious 7.0.0)
-
createTempDir
public static java.io.File createTempDir(boolean deleteDirectoryOnceItIsNoLongerReferenced) throws java.io.IOException
Creates a temporary directory. If Geneious is running when this method is invoked, the parent directory of the returned temp directory will be the user's data directory which will have its contents deleted when Geneious is next started. If Geneious is not running when this method is invoked, the temp directory is created inside the standard system temporary directory. If deleteDirectoryOnceItIsNoLongerReferenced is true, then the returned directory will be automatically deleted once the returned File or the String obtained fromFile.getPath()
orFile.toString()
are no longer referenced, even if there are still referenced files within that directory. The returned directory will already have had its path made canonical (viaFile.getCanonicalPath()
) so there is no need to call that method on the returned file and doing so will not hold a reference to the given directory potentially causing it to be deleted early. No matter what the value of deleteDirectoryOnceItIsNoLongerReferenced, the directory will be deleted if it still exists when the user next starts Geneious. The user may change their data directory from preferences while Geneious is running, so plugins should not assume the path of the returned directory to necessarily be the same from separate calls to this method.- Parameters:
deleteDirectoryOnceItIsNoLongerReferenced
- - see above.- Returns:
- a File that will be automatically deleted when the given File object (or it's path) are no longer referenced.
- Throws:
java.io.IOException
- If a file could not be created- Since:
- API 4.60 (Geneious 5.6.0)
-
createHardLink
public static boolean createHardLink(java.io.File existingFile, java.io.File newFile)
Creates a file system hard link. Not all file systems support hard links. For example on a Windows platform, NTFS does support hard links, but FAT32 doesn't. A hard link has the effect of creating multiple names for the same file, causing an aliasing effect: if the file is opened by one of its names, and changes are made to its content, then these changes will also be visible when the file is opened by an alternative name. A file is not removed from the file system until all names it is known by are deleted. Windows has unusual and very annoying behaviour when dealing with hard links. Consider a file called file1.txt with a hard link called file2.txt to it. Then while anything has file2.txt open, attempts to delete file1.txt will fail. More annoyingly, if 1 thread is in the process of deleting file1, another thread that tries to open file2 for reading may fail with a "The process cannot access the file because it is being used by another process" error. Waiting a fraction of a second and trying again generally fixes this, so it is recommended that any code that tries to open a file that has been hard linked to, or has had a hard link to it created, it should usenewRandomAccessFileWithRetry(java.io.File, String)
ornewFileInputStreamWithRetry(java.io.File)
- Parameters:
existingFile
- a currently existing file to link tonewFile
- the new file to create which should link to the existing file- Returns:
- true if the link is successfully created. false if not or if this thread is interrupted while attempting to create the hard link or if there is a timout creating the hard link
- Since:
- API 4.40 (Geneious 5.4.0)
-
newRandomAccessFileWithRetry
public static java.io.RandomAccessFile newRandomAccessFileWithRetry(java.io.File file, java.lang.String mode) throws java.io.FileNotFoundException
Attempts to create a new RandomAccessFile, but if it fails, retries a few times until it succeeds. This is recommended if you attempt to open files that have been created as hard links or have had hard links to them created. SeecreateHardLink(java.io.File, java.io.File)
- Parameters:
file
- the file to openmode
- the access mode. SeeRandomAccessFile(java.io.File, String)
- Returns:
- a new RandomAccessFile
- Throws:
java.io.FileNotFoundException
- ifRandomAccessFile(java.io.File, String)
still throws a FileNotFoundException after a few attempts- Since:
- API 4.40 (Geneious 5.4.0)
-
newFileInputStreamWithRetry
public static java.io.FileInputStream newFileInputStreamWithRetry(java.io.File file) throws java.io.FileNotFoundException
Attempts to create a new FileInputStream, but if it fails, retries a few times until it succeeds. This is recommended if you attempt to open files that have been created as hard links or have had hard links to them created. SeecreateHardLink(java.io.File, java.io.File)
- Parameters:
file
- the file to open- Returns:
- a new FileInputStream
- Throws:
java.io.FileNotFoundException
- ifFileInputStream(java.io.File)
still throws a FileNotFoundException after a few attempts- Since:
- API 4.40 (Geneious 5.4.0)
- See Also:
newFileOutputStreamWithRetry(java.io.File)
-
newFileOutputStreamWithRetry
public static java.io.FileOutputStream newFileOutputStreamWithRetry(java.io.File file) throws java.io.FileNotFoundException
Attempts to create a new FileOutputStream, but if it fails with a FileNotFoundException, retries a few times until it succeeds. This is recommended if you attempt to open files that have been created as hard links or have had hard links to them created. SeecreateHardLink(java.io.File, java.io.File)
. This is also useful when trying to write to a file of the same name that was recently deleted on systems with virus scanners. The virus scanner may still have the original file open, preventing it from being deleted immediately and causing Windows to deny access to writing to a new file with the same name. See comments at http://stackoverflow.com/questions/8670317/why-cant-i-create-a-txt-file-after-having-just-deleted-it- Parameters:
file
- the file to open- Returns:
- a new FileOutputStream
- Throws:
java.io.FileNotFoundException
- ifFileOutputStream(java.io.File)
still throws a FileNotFoundException after a few attempts- Since:
- API 4.701 (Geneious 7.0.1)
- See Also:
newFileInputStreamWithRetry(java.io.File)
-
getResourceForClass
public static java.io.File getResourceForClass(java.lang.Class cl, java.lang.String resourceName)
for grabbing resources based on a directory hirarchy identical to the one that the classcl
is found in.- Parameters:
cl
- for example aGeneiousPlugin
subclassresourceName
- for example a file needed by the plugin- Returns:
- The needed file
- Throws:
java.lang.IllegalStateException
- if the resource is not found.- Since:
- API 4.58 (Geneious 5.5.50)
-
-