Class FileUtilSupport
- All Implemented Interfaces:
BeanInterface,PropertyChangeFirer,PropertyChangeProvider
FileUtil static API while providing
PropertyChangeSupport for listening to changes in the
paths. Also provides the underlying implementation of all FileUtil methods so
they can be exposed to scripts as an object methods instead of as static
methods of a class.-
Field Summary
Fields inherited from class jmri.beans.Bean
propertyChangeSupport -
Method Summary
Modifier and TypeMethodDescriptionvoidappendTextToFile(File file, String text) Simple helper method to just append a text string to the end of the given filename.voidBackup a file.voidCopy a file or directory.voidcreateDirectory(File dir) Create a directory if required.voidcreateDirectory(String path) Create a directory if required.booleanRecursively delete a path.findExternalFilename(String path) Get the URL of a portable filename if it can be located usingfindURI(java.lang.String)Find all files matching the given name under the given root directory within both the user and installed file locations.findFiles(String name, String root, FileUtil.Location location) Find all files matching the given name under the given root directory within the specified location.findInputStream(String path) Search for a file or JAR resource by name and return theInputStreamfor that file.findInputStream(String path, String... searchPaths) Search for a file or JAR resource by name and return theInputStreamfor that file.findInputStream(String path, FileUtil.Location locations) Search for a file or JAR resource by name and return theInputStreamfor that file.findInputStream(String path, FileUtil.Location locations, String... searchPaths) Search for a file or JAR resource by name and return theInputStreamfor that file.Search for a file or JAR resource by name and return theURIfor that file.Search for a file or JAR resource by name and return theURIfor that file.findURI(String path, FileUtil.Location locations) Search for a file or JAR resource by name and return theURIfor that file.findURI(String path, FileUtil.Location locations, String... searchPaths) Search for a file or JAR resource by name and return theURIfor that file.Search for a file or JAR resource by name and return theURLfor that file.Search for a file or JAR resource by name and return theURLfor that file.findURL(String path, FileUtil.Location locations) Search for a file or JAR resource by name and return theURLfor that file.findURL(String path, FileUtil.Location locations, String... searchPaths) Search for a file or JAR resource by name and return theURLfor that file.getAbsoluteFilename(String path) Convert a portable filename into an absolute filename, usingProfileManager.getActiveProfile()as the base.getAbsoluteFilename(Profile profile, String path) Convert a portable filename into an absolute filename.Get the JMRI cache location, ensuring its existence.static FileUtilSupportGet the default instance of a FileUtilSupport object.getExternalFilename(String pName) Get the resource file corresponding to a name.getExternalFilename(Profile profile, String pName) Get the resource file corresponding to a name.Get theFilethat path refers to.Get theFilethat path refers to.Get the user's home directory.Get the JMRI distribution jar file.getPortableFilename(File file) Convert a File object's path to our preferred storage form.getPortableFilename(File file, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a File object's path to our preferred storage form.getPortableFilename(String filename) Convert a filename string to our preferred storage form.getPortableFilename(String filename, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a filename string to our preferred storage form.getPortableFilename(Profile profile, File file) Convert a File object's path to our preferred storage form.getPortableFilename(Profile profile, File file, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a File object's path to our preferred storage form.getPortableFilename(Profile profile, String filename) Convert a filename string to our preferred storage form.getPortableFilename(Profile profile, String filename, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a filename string to our preferred storage form.Get the preferences directory.Get the profile directory.getProfilePath(Profile profile) Get the profile directory.Get the JMRI program directory.Get the path to the scripts directory.getScriptsPath(Profile profile) Get the path to the scripts directory.Get theFilethat path refers to.Get theURLthat path refers to.Get the user's files directory.getUserFilesPath(Profile profile) Get the user's files directory.Get the resources directory within the user's files directory.booleanisPortableFilename(String filename) Test if the given filename is a portable filename.voidLog all paths at the INFO level.Read a text file into a String.Read a text URL into a String.protected static voidvoidRotate a file and its backups, retaining only a set number of backups.sanitizeFilename(String name) Replaces most non-alphanumeric characters in name with an underscore.voidsetProgramPath(File path) Set the JMRI program directory.voidsetProgramPath(String path) Set the JMRI program directory.voidsetScriptsPath(Profile profile, String path) Set the path to python scripts.voidsetUserFilesPath(Profile profile, String path) Set the user's files directory.Return theURIfor a given URLMethods inherited from class jmri.beans.Bean
addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, getPropertyChangeListeners, isNotifyOnEDT, removePropertyChangeListener, removePropertyChangeListenerMethods inherited from class jmri.beans.UnboundBean
getIndexedProperty, getProperty, getPropertyNames, hasIndexedProperty, hasProperty, setIndexedProperty, setProperty
-
Method Details
-
getDefault
Get the default instance of a FileUtilSupport object.Unlike most implementations of getDefault(), this does not return an object held by
InstanceManagerdue to the need for this default instance to be available prior to the creation of an InstanceManager.- Returns:
- the default FileUtilSupport instance, creating it if necessary
-
resetInstance
-
getFile
Get theFilethat path refers to. Throws aFileNotFoundExceptionif the file cannot be found instead of returning null (as File would). UsegetURI(java.lang.String)orgetURL(java.lang.String)instead of this method if possible.- Parameters:
path- the path to find- Returns:
Fileat path- Throws:
FileNotFoundException- if path cannot be found- See Also:
-
getFile
@Nonnull @CheckReturnValue public File getFile(@CheckForNull Profile profile, @Nonnull String path) throws FileNotFoundException Get theFilethat path refers to. Throws aFileNotFoundExceptionif the file cannot be found instead of returning null (as File would). UsegetURI(java.lang.String)orgetURL(java.lang.String)instead of this method if possible.- Parameters:
profile- the profile to use as a basepath- the path to find- Returns:
Fileat path- Throws:
FileNotFoundException- if path cannot be found- See Also:
-
getURI
Get theFilethat path refers to. Throws aFileNotFoundExceptionif the file cannot be found instead of returning null (as File would).- Parameters:
path- the path to find- Returns:
Fileat path- Throws:
FileNotFoundException- if path cannot be found- See Also:
-
getURL
Get theURLthat path refers to. Throws aFileNotFoundExceptionif the URL cannot be found instead of returning null.- Parameters:
path- the path to find- Returns:
URLat path- Throws:
FileNotFoundException- if path cannot be found- See Also:
-
getURL
Convenience method to get theURLfrom aURI. Logs errors and returns null if any exceptions are thrown by the conversion.- Parameters:
uri- The URI to convert.- Returns:
- URL or null if any errors exist.
-
findFiles
@Nonnull @CheckReturnValue public Set<File> findFiles(@Nonnull String name, @Nonnull String root) throws IllegalArgumentException Find all files matching the given name under the given root directory within both the user and installed file locations.- Parameters:
name- the name of the file to findroot- the relative path to a directory in either or both of the user or installed file locations; use a single period character to refer to the root of the user or installed file locations- Returns:
- a set of found files or an empty set if no matching files were found
- Throws:
IllegalArgumentException- if the name is not a relative path, is empty, or contains path separators; or if the root is not a relative path, is empty, or contains a parent directory (..)NullPointerException- if any parameter is null
-
findFiles
@Nonnull @CheckReturnValue public Set<File> findFiles(@Nonnull String name, @Nonnull String root, @Nonnull FileUtil.Location location) Find all files matching the given name under the given root directory within the specified location.- Parameters:
name- the name of the file to findroot- the relative path to a directory in either or both of the user or installed file locations; use a single period character to refer to the root of the locationlocation- the location to search within- Returns:
- a set of found files or an empty set if no matching files were found
- Throws:
IllegalArgumentException- if the name is not a relative path, is empty, or contains path separators; if the root is not a relative path, is empty, or contains a parent directory (..); or if the location isFileUtil.Location.NONENullPointerException- if any parameter is null
-
getExternalFilename
Get the resource file corresponding to a name. There are five cases:- Starts with "resource:", treat the rest as a pathname relative to the program directory (deprecated; see "program:" below)
- Starts with "program:", treat the rest as a relative pathname below the program directory
- Starts with "preference:", treat the rest as a relative path below the user's files directory
- Starts with "settings:", treat the rest as a relative path below the JMRI system preferences directory
- Starts with "home:", treat the rest as a relative path below the user.home directory
- Starts with "file:", treat the rest as a relative path below the resource directory in the preferences directory (deprecated; see "preference:" above)
- Starts with "profile:", treat the rest as a relative path below the
profile directory as specified in the
active
Profile - Starts with "scripts:", treat the rest as a relative path below the scripts directory
- Otherwise, treat the name as a relative path below the program directory
ProfileManager.getActiveProfile()as the base.- Parameters:
pName- the name, possibly starting with file:, home:, profile:, program:, preference:, scripts:, settings, or resource:- Returns:
- Absolute file name to use. This will include system-specific file separators.
- Since:
- 2.7.2
-
getExternalFilename
@Nonnull @CheckReturnValue public String getExternalFilename(@CheckForNull Profile profile, @Nonnull String pName) Get the resource file corresponding to a name. There are five cases:- Starts with "resource:", treat the rest as a pathname relative to the program directory (deprecated; see "program:" below)
- Starts with "program:", treat the rest as a relative pathname below the program directory
- Starts with "preference:", treat the rest as a relative path below the user's files directory
- Starts with "settings:", treat the rest as a relative path below the JMRI system preferences directory
- Starts with "home:", treat the rest as a relative path below the user.home directory
- Starts with "file:", treat the rest as a relative path below the resource directory in the preferences directory (deprecated; see "preference:" above)
- Starts with "profile:", treat the rest as a relative path below the
profile directory as specified in the
active
Profile - Starts with "scripts:", treat the rest as a relative path below the scripts directory
- Otherwise, treat the name as a relative path below the program directory
- Parameters:
profile- the Profile to use as a basepName- the name, possibly starting with file:, home:, profile:, program:, preference:, scripts:, settings, or resource:- Returns:
- Absolute file name to use. This will include system-specific file separators.
- Since:
- 4.17.3
-
getAbsoluteFilename
Convert a portable filename into an absolute filename, usingProfileManager.getActiveProfile()as the base.- Parameters:
path- the portable filename- Returns:
- An absolute filename
-
getAbsoluteFilename
@Nonnull @CheckReturnValue public String getAbsoluteFilename(@CheckForNull Profile profile, @Nonnull String path) Convert a portable filename into an absolute filename.- Parameters:
profile- the profile to use as the basepath- the portable filename- Returns:
- An absolute filename
-
getPortableFilename
Convert a File object's path to our preferred storage form.This is the inverse of
getFile(String pName). Deprecated forms are not created.- Parameters:
file- File at path to be represented- Returns:
- Filename for storage in a portable manner. This will include portable, not system-specific, file separators.
- Since:
- 2.7.2
-
getPortableFilename
@Nonnull @CheckReturnValue public String getPortableFilename(@Nonnull File file, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a File object's path to our preferred storage form.This is the inverse of
getFile(String pName). Deprecated forms are not created.This method supports a specific use case concerning profiles and other portable paths that are stored within the User files directory, which will cause the
ProfileManagerto write an incorrect path for the current profile orFileLocationPaneXmlto write an incorrect path for the Users file directory. In most cases, the use ofgetPortableFilename(java.io.File)is preferable.- Parameters:
file- File at path to be representedignoreUserFilesPath- true if paths in the User files path should be stored as absolute paths, which is often not desirable.ignoreProfilePath- true if paths in the profile should be stored as absolute paths, which is often not desirable.- Returns:
- Storage format representation
- Since:
- 3.5.5
-
getPortableFilename
Convert a filename string to our preferred storage form.This is the inverse of
getExternalFilename(String pName). Deprecated forms are not created.- Parameters:
filename- Filename to be represented- Returns:
- Filename for storage in a portable manner
- Since:
- 2.7.2
-
getPortableFilename
@Nonnull @CheckReturnValue public String getPortableFilename(@Nonnull String filename, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a filename string to our preferred storage form.This is the inverse of
getExternalFilename(String pName). Deprecated forms are not created.This method supports a specific use case concerning profiles and other portable paths that are stored within the User files directory, which will cause the
ProfileManagerto write an incorrect path for the current profile orFileLocationPaneXmlto write an incorrect path for the Users file directory. In most cases, the use ofgetPortableFilename(java.io.File)is preferable.- Parameters:
filename- Filename to be representedignoreUserFilesPath- true if paths in the User files path should be stored as absolute paths, which is often not desirable.ignoreProfilePath- true if paths in the profile path should be stored as absolute paths, which is often not desirable.- Returns:
- Storage format representation
- Since:
- 3.5.5
-
getPortableFilename
@Nonnull @CheckReturnValue public String getPortableFilename(@CheckForNull Profile profile, @Nonnull File file) Convert a File object's path to our preferred storage form.This is the inverse of
getFile(String pName). Deprecated forms are not created.- Parameters:
profile- Profile to use as basefile- File at path to be represented- Returns:
- Filename for storage in a portable manner. This will include portable, not system-specific, file separators.
- Since:
- 4.17.3
-
getPortableFilename
@Nonnull @CheckReturnValue public String getPortableFilename(@CheckForNull Profile profile, @Nonnull File file, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a File object's path to our preferred storage form.This is the inverse of
getFile(String pName). Deprecated forms are not created.This method supports a specific use case concerning profiles and other portable paths that are stored within the User files directory, which will cause the
ProfileManagerto write an incorrect path for the current profile orFileLocationPaneXmlto write an incorrect path for the Users file directory. In most cases, the use ofgetPortableFilename(java.io.File)is preferable.- Parameters:
profile- Profile to use as basefile- File at path to be representedignoreUserFilesPath- true if paths in the User files path should be stored as absolute paths, which is often not desirable.ignoreProfilePath- true if paths in the profile should be stored as absolute paths, which is often not desirable.- Returns:
- Storage format representation
- Since:
- 3.5.5
-
getPortableFilename
@Nonnull @CheckReturnValue public String getPortableFilename(@CheckForNull Profile profile, @Nonnull String filename) Convert a filename string to our preferred storage form.This is the inverse of
getExternalFilename(String pName). Deprecated forms are not created.- Parameters:
profile- Profile to use as basefilename- Filename to be represented- Returns:
- Filename for storage in a portable manner
- Since:
- 4.17.3
-
getPortableFilename
@Nonnull @CheckReturnValue public String getPortableFilename(@CheckForNull Profile profile, @Nonnull String filename, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a filename string to our preferred storage form.This is the inverse of
getExternalFilename(String pName). Deprecated forms are not created.This method supports a specific use case concerning profiles and other portable paths that are stored within the User files directory, which will cause the
ProfileManagerto write an incorrect path for the current profile orFileLocationPaneXmlto write an incorrect path for the Users file directory. In most cases, the use ofgetPortableFilename(java.io.File)is preferable.- Parameters:
profile- Profile to use as basefilename- Filename to be representedignoreUserFilesPath- true if paths in the User files path should be stored as absolute paths, which is often not desirable.ignoreProfilePath- true if paths in the profile path should be stored as absolute paths, which is often not desirable.- Returns:
- Storage format representation
- Since:
- 4.17.3
-
isPortableFilename
Test if the given filename is a portable filename.Note that this method may return a false positive if the filename is a file: URL.
- Parameters:
filename- the name to test- Returns:
- true if filename is portable
-
getHomePath
Get the user's home directory.- Returns:
- User's home directory as a String
-
getUserFilesPath
Get the user's files directory. If not set by the user, this is the same as the profile path returned byProfileManager.getActiveProfile(). Note that if the profile path has been set to null, that returns the preferences directory, seegetProfilePath().- Returns:
- User's files directory
- See Also:
-
getUserFilesPath
Get the user's files directory. If not set by the user, this is the same as the profile path. Note that if the profile path has been set to null, that returns the preferences directory, seegetProfilePath().- Parameters:
profile- the profile to use- Returns:
- User's files directory
- See Also:
-
setUserFilesPath
Set the user's files directory.- Parameters:
profile- the profile to set the user's files directory forpath- The path to the user's files directory using system-specific separators- See Also:
-
getProfilePath
Get the profile directory. If not set, provide the preferences path.- Parameters:
profile- the Profile to use as a base- Returns:
- Profile directory using system-specific separators
- See Also:
-
getProfilePath
Get the profile directory. If not set, provide the preferences path. Uses the Profile returned byProfileManager.getActiveProfile()as a base.- Returns:
- Profile directory using system-specific separators
- See Also:
-
getPreferencesPath
Get the preferences directory. This directory is set based on the OS and is not normally settable by the user.- On Microsoft Windows systems, this is
JMRIin the User's home directory. - On OS X systems, this is
Library/Preferences/JMRIin the User's home directory. - On Linux, Solaris, and other UNIXes, this is
.jmriin the User's home directory. - This can be overridden with by setting the
jmri.prefsdirJava property when starting JMRI.
getHomePath()to get the User's home directory.- Returns:
- Path to the preferences directory using system-specific separators.
- See Also:
- On Microsoft Windows systems, this is
-
getCacheDirectory
Get the JMRI cache location, ensuring its existence.This is not part of the
FileUtilAPI since it should generally be accessed usingProfileUtils.getCacheDirectory(jmri.profile.Profile, java.lang.Class).Uses the following locations (where [version] is from
Version.getCanonicalVersion()):- System Property (if set)
- value of jmri_default_cachedir
- macOS
- ~/Library/Caches/JMRI/[version]
- Windows
- %Local AppData%/JMRI/[version]
- UNIX/Linux/POSIX
- ${XDG_CACHE_HOME}/JMRI/[version] or $HOME/.cache/JMRI/[version]
- Fallback
- JMRI portable path setting:cache/[version]
- Returns:
- the cache directory for this version of JMRI
-
getProgramPath
Get the JMRI program directory.If the program directory has not been previously set, first sets the program directory to the value specified in the Java System property
jmri.path.programIf this property is unset, finds from jar or class files location.
If this fails, returns
..- Returns:
- JMRI program directory as a String.
-
setProgramPath
Set the JMRI program directory.Convenience method that calls
setProgramPath(java.io.File)with the passed in path.- Parameters:
path- the path to the JMRI installation
-
setProgramPath
Set the JMRI program directory.If set, allows JMRI to be loaded from locations other than the directory containing JMRI resources. This must be set very early in the process of loading JMRI (prior to loading any other JMRI code) to be meaningfully used.
- Parameters:
path- the path to the JMRI installation
-
getUserResourcePath
Get the resources directory within the user's files directory.- Returns:
- path to [user's file]/resources/ using system-specific separators
-
logFilePaths
Log all paths at the INFO level. -
getScriptsPath
Get the path to the scripts directory. If not set previously withsetScriptsPath(jmri.profile.Profile, java.lang.String), this is the "jython" subdirectory in the program directory. Uses the Profile returned byProfileManager.getActiveProfile()as the base.- Returns:
- the scripts directory using system-specific separators
-
getScriptsPath
Get the path to the scripts directory. If not set previously withsetScriptsPath(jmri.profile.Profile, java.lang.String), this is the "jython" subdirectory in the program directory.- Parameters:
profile- the Profile to use as the base- Returns:
- the path to scripts directory using system-specific separators
-
setScriptsPath
Set the path to python scripts.- Parameters:
profile- the profile to use as a basepath- the scriptsPaths to set; null resets to the default, defined ingetScriptsPath()
-
findExternalFilename
Get the URL of a portable filename if it can be located usingfindURI(java.lang.String)- Parameters:
path- the path to find- Returns:
- URL of portable or absolute path
-
findInputStream
Search for a file or JAR resource by name and return theInputStreamfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...). No limits are placed on search locations.- Parameters:
path- The relative path of the file or resource- Returns:
- InputStream or null.
- See Also:
-
findInputStream
Search for a file or JAR resource by name and return theInputStreamfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...). No limits are placed on search locations.- Parameters:
path- The relative path of the file or resourcesearchPaths- a list of paths to search for the path in- Returns:
- InputStream or null.
- See Also:
-
findInputStream
Search for a file or JAR resource by name and return theInputStreamfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...).- Parameters:
path- The relative path of the file or resourcelocations- The type of locations to limit the search to- Returns:
- InputStream or null.
- See Also:
-
findInputStream
public InputStream findInputStream(@Nonnull String path, @Nonnull FileUtil.Location locations, @Nonnull String... searchPaths) Search for a file or JAR resource by name and return theInputStreamfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...).- Parameters:
path- The relative path of the file or resourcelocations- The type of locations to limit the search tosearchPaths- a list of paths to search for the path in- Returns:
- InputStream or null.
- See Also:
-
findURI
Search for a file or JAR resource by name and return theURIfor that file. Search order is defined byfindURI(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...). No limits are placed on search locations.- Parameters:
path- The relative path of the file or resource.- Returns:
- The URI or null.
- See Also:
-
findURI
Search for a file or JAR resource by name and return theURIfor that file. Search order is defined byfindURI(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...). No limits are placed on search locations.Note that if the file for path is not found in one of the searchPaths, all standard locations are also be searched through to find the file. If you need to limit the locations where the file can be found use
findURI(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...).- Parameters:
path- The relative path of the file or resourcesearchPaths- a list of paths to search for the path in- Returns:
- The URI or null
- See Also:
-
findURI
Search for a file or JAR resource by name and return theURIfor that file. Search order is defined byfindURI(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...).- Parameters:
path- The relative path of the file or resourcelocations- The types of locations to limit the search to- Returns:
- The URI or null
- See Also:
-
findURI
public URI findURI(@Nonnull String path, @Nonnull FileUtil.Location locations, @Nonnull String... searchPaths) Search for a file or JAR resource by name and return theURIfor that file.Search order is:
- For any provided searchPaths, iterate over the searchPaths by
prepending each searchPath to the path and following the following search
order:
- As a
Filein the user preferences directory - As a File in the current working directory (usually, but not always the JMRI distribution directory)
- As a File in the JMRI distribution directory
- As a resource in jmri.jar
- As a
- If the file or resource has not been found in the searchPaths, search in the four locations listed without prepending any path
- As a File with an absolute path
The
locationsparameter limits the above logic by limiting the location searched.FileUtil.Location.ALLwill not place any limits on the searchFileUtil.Location.NONEeffectively requires thatpathbe a portable pathnameFileUtil.Location.INSTALLEDlimits the search to theFileUtil.PROGRAMdirectory and JARs in the class pathFileUtil.Location.USERlimits the search to theFileUtil.PREFERENCES,FileUtil.PROFILE, andFileUtil.SETTINGSdirectories (in that order)
- Parameters:
path- The relative path of the file or resourcelocations- The types of locations to limit the search tosearchPaths- a list of paths to search for the path in- Returns:
- The URI or null
- See Also:
- For any provided searchPaths, iterate over the searchPaths by
prepending each searchPath to the path and following the following search
order:
-
findURL
Search for a file or JAR resource by name and return theURLfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...). No limits are placed on search locations.- Parameters:
path- The relative path of the file or resource.- Returns:
- The URL or null.
- See Also:
-
findURL
Search for a file or JAR resource by name and return theURLfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...). No limits are placed on search locations.- Parameters:
path- The relative path of the file or resourcesearchPaths- a list of paths to search for the path in- Returns:
- The URL or null
- See Also:
-
findURL
Search for a file or JAR resource by name and return theURLfor that file. Search order is defined byfindURL(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...).- Parameters:
path- The relative path of the file or resourcelocations- The types of locations to limit the search to- Returns:
- The URL or null
- See Also:
-
findURL
public URL findURL(@Nonnull String path, @Nonnull FileUtil.Location locations, @Nonnull String... searchPaths) Search for a file or JAR resource by name and return theURLfor that file.Search order is:
- For any provided searchPaths, iterate over the searchPaths by
prepending each searchPath to the path and following the following search
order:
- As a
Filein the user preferences directory - As a File in the current working directory (usually, but not always the JMRI distribution directory)
- As a File in the JMRI distribution directory
- As a resource in jmri.jar
- As a
- If the file or resource has not been found in the searchPaths, search in the four locations listed without prepending any path
The
locationsparameter limits the above logic by limiting the location searched.FileUtil.Location.ALLwill not place any limits on the searchFileUtil.Location.NONEeffectively requires thatpathbe a portable pathnameFileUtil.Location.INSTALLEDlimits the search to theFileUtil.PROGRAMdirectory and JARs in the class pathFileUtil.Location.USERlimits the search to theFileUtil.PROFILEdirectory
- Parameters:
path- The relative path of the file or resourcelocations- The types of locations to limit the search tosearchPaths- a list of paths to search for the path in- Returns:
- The URL or null
- See Also:
- For any provided searchPaths, iterate over the searchPaths by
prepending each searchPath to the path and following the following search
order:
-
urlToURI
Return theURIfor a given URL- Parameters:
url- the URL- Returns:
- a URI or null if the conversion would have caused a
URISyntaxException
-
fileToURL
Return theURLfor a givenFile. This method catches aMalformedURLExceptionand returns null in its place, since we really do not expect a File object to ever give a malformed URL. This method exists solely so implementing classes do not need to catch that exception.- Parameters:
file- The File to convert.- Returns:
- a URL or null if the conversion would have caused a MalformedURLException
-
getJmriJarFile
Get the JMRI distribution jar file.- Returns:
- the JAR file containing the JMRI library or null if not running from a JAR file
-
readFile
Read a text file into a String.- Parameters:
file- The text file.- Returns:
- The contents of the file.
- Throws:
IOException- if the file cannot be read
-
readURL
Read a text URL into a String.- Parameters:
url- The text URL.- Returns:
- The contents of the file.
- Throws:
IOException- if the URL cannot be read
-
sanitizeFilename
Replaces most non-alphanumeric characters in name with an underscore.- Parameters:
name- The filename to be sanitized.- Returns:
- The sanitized filename.
-
createDirectory
Create a directory if required. Any parent directories will also be created.- Parameters:
path- directory to create
-
createDirectory
Create a directory if required. Any parent directories will also be created.- Parameters:
dir- directory to create
-
delete
Recursively delete a path. It is recommended to useFiles.delete(java.nio.file.Path)orFiles.deleteIfExists(java.nio.file.Path)for files.- Parameters:
path- path to delete- Returns:
- true if path was deleted, false otherwise
-
copy
Copy a file or directory. It is recommended to useFiles.copy(java.nio.file.Path, java.io.OutputStream)for files.- Parameters:
source- the file or directory to copydest- must be the file or directory, not the containing directory- Throws:
IOException- if file cannot be copied
-
appendTextToFile
Simple helper method to just append a text string to the end of the given filename. The file will be created if it does not exist.- Parameters:
file- File to append text totext- Text to append- Throws:
IOException- if file cannot be written to
-
backup
Backup a file. The backup is in the same location as the original file, has the extension.bakappended to the file name, and up to four revisions are retained. The lowest numbered revision is the most recent.- Parameters:
file- the file to backup- Throws:
IOException- if a backup cannot be created
-
rotate
Rotate a file and its backups, retaining only a set number of backups.- Parameters:
file- the file to rotatemax- maximum number of backups to retainextension- The extension to use for the rotations. If null or an empty string, the rotation number is used as the extension.- Throws:
IOException- if a backup cannot be createdIllegalArgumentException- if max is less than one- See Also:
-