Class FileUtil
All methods in this class call the identical method from the default instance
of FileUtilSupport.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe types of locations to use when falling back on default locations infindURI(java.lang.String, jmri.util.FileUtil.Location, java.lang.String...).static classPropertyChangeEvents for properties that are Profile-specific use a Property to enclose both the Profile and the value of the property. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringPortable reference to the user's home directory.static final StringPortable reference to the JMRI user's files and preferences directory.static final StringPortable reference to the current profile directory.static final StringPortable reference to items in the JMRI program directory.static final StringPortable reference to the current scripts directory.static final charThe portable file path component separator.static final StringPortable reference to the JMRI applications preferences directory. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendTextToFile(File file, String text) Simple helper method to just append a text string to the end of the given filename.static voidBackup a file.static voidCopy a file or directory.static voidcreateDirectory(File dir) Create a directory if required.static voidcreateDirectory(String path) Create a directory if required.static booleanRecursively delete a path.static URLstatic URIfindExternalFilename(String path) Get the URL of a portable filename if it can be located usingfindURL(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.static InputStreamfindInputStream(String path) Search for a file or JAR resource by name and return theInputStreamfor that file.static InputStreamfindInputStream(String path, String... searchPaths) Search for a file or JAR resource by name and return theInputStreamfor that file.static InputStreamfindInputStream(String path, FileUtil.Location locations) Search for a file or JAR resource by name and return theInputStreamfor that file.static InputStreamfindInputStream(String path, FileUtil.Location locations, String... searchPaths) Search for a file or JAR resource by name and return theInputStreamfor that file.static URISearch for a file or JAR resource by name and return theURIfor that file.static URISearch for a file or JAR resource by name and return theURIfor that file.static URIfindURI(String path, FileUtil.Location locations) Search for a file or JAR resource by name and return theURIfor that file.static URIfindURI(String path, FileUtil.Location locations, String... searchPaths) Search for a file or JAR resource by name and return theURIfor that file.static URLSearch for a file or JAR resource by name and return theURLfor that file.static URLSearch for a file or JAR resource by name and return theURLfor that file.static URLfindURL(String path, FileUtil.Location locations) Search for a file or JAR resource by name and return theURLfor that file.static URLfindURL(String path, FileUtil.Location locations, String... searchPaths) Search for a file or JAR resource by name and return theURLfor that file.static StringgetAbsoluteFilename(String path) Convert a portable filename into an absolute filename, usingProfileManager.getActiveProfile()as the base.static StringgetAbsoluteFilename(Profile profile, String path) Convert a portable filename into an absolute filename.static FileUtilSupportGet the default instance of FileUtilSupport.static StringgetExternalFilename(String pName) Get the resource file corresponding to a name.static StringgetExternalFilename(Profile profile, String pName) Get the resource file corresponding to a name.static FileGet theFilethat path refers to.static FileGet theFilethat path refers to.static StringGet the user's home directory.static StringgetPortableFilename(File file) Convert a File object's path to our preferred storage form.static StringgetPortableFilename(File file, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a File object's path to our preferred storage form.static StringgetPortableFilename(String filename) Convert a filename string to our preferred storage form.static StringgetPortableFilename(String filename, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a filename string to our preferred storage form.static StringgetPortableFilename(Profile profile, File file) Convert a File object's path to our preferred storage form.static StringgetPortableFilename(Profile profile, File file, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a File object's path to our preferred storage form.static StringgetPortableFilename(Profile profile, String filename) Convert a filename string to our preferred storage form.static StringgetPortableFilename(Profile profile, String filename, boolean ignoreUserFilesPath, boolean ignoreProfilePath) Convert a filename string to our preferred storage form.static StringGet the preferences directory.static StringGet the profile directory.static StringgetProfilePath(Profile profile) Get the profile directory.static StringGet the JMRI program directory.static StringGet the path to the scripts directory using the Profile returned byProfileManager.getActiveProfile()as the base.static StringgetScriptsPath(Profile profile) Get the path to the scripts directory.static URIGet theFilethat path refers to.static URLGet theURLthat path refers to.static URLstatic StringGet the user's files directory.static StringgetUserFilesPath(Profile profile) Get the user's files directory.static StringGet the resources directory within the user's files directory.static booleanisPortableFilename(String filename) Test if the given filename is a portable filename.static JarFileGet the JMRI distribution jar file.static voidLog all paths at the INFO level.static StringRead a text file into a String.static StringRead a text URL into a String.static voidRotate a file and its backups, retaining only a set number of backups.static StringsanitizeFilename(String name) Replaces most non-alphanumeric characters in name with an underscore.static voidsetProgramPath(File path) Set the JMRI program directory.static voidsetProgramPath(String path) Set the JMRI program directory.static voidsetScriptsPath(Profile profile, String path) Set the path to python scripts.static voidsetUserFilesPath(Profile profile, String path) Set the user's files directory.static URIReturn theURIfor a given URL
-
Field Details
-
PROGRAM
Portable reference to items in the JMRI program directory.- See Also:
-
PREFERENCES
Portable reference to the JMRI user's files and preferences directory.- See Also:
-
SETTINGS
Portable reference to the JMRI applications preferences directory.- See Also:
-
HOME
Portable reference to the user's home directory.- See Also:
-
PROFILE
Portable reference to the current profile directory.- See Also:
-
SCRIPTS
Portable reference to the current scripts directory.- See Also:
-
SEPARATOR
The portable file path component separator.- See Also:
-
-
Method Details
-
getFile
@Nonnull @CheckReturnValue public static File getFile(@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:
path- the path to find- Returns:
Fileat path- Throws:
FileNotFoundException- if path cannot be found- See Also:
-
getFile
@Nonnull @CheckReturnValue public static 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
@Nonnull @CheckReturnValue public static URI getURI(@Nonnull String path) throws FileNotFoundException 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
@Nonnull @CheckReturnValue public static URL getURL(@Nonnull String path) throws FileNotFoundException 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 static 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 static 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 "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 "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 home:, profile:, program:, preference:, scripts:, or settings:- Returns:
- Absolute file name to use, or null. This will include system-specific file separators.
- Since:
- 2.7.2
-
getExternalFilename
@Nonnull @CheckReturnValue public static String getExternalFilename(@CheckForNull Profile profile, @Nonnull String pName) Get the resource file corresponding to a name. There are five cases:- 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 "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 base.pName- the name, possibly starting with home:, profile:, program:, preference:, scripts:, or settings:- Returns:
- Absolute file name to use, or null. 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 static String getAbsoluteFilename(@CheckForNull Profile profile, @Nonnull String path) Convert a portable filename into an absolute filename.- Parameters:
profile- the profile to use 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 static 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 static 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 static 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 a 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 static 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 a 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:
- 4.17.3
-
getPortableFilename
@Nonnull @CheckReturnValue public static 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- the Profile to use as a basefilename- Filename to be represented- Returns:
- Filename for storage in a portable manner
- Since:
- 4.17.3
-
getPortableFilename
@Nonnull @CheckReturnValue public static 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- the profile to use as a 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.- 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 for the Profile specified byProfileManager.getActiveProfile().- Returns:
- User's files directory as a String
- See Also:
-
getUserFilesPath
Get the user's files directory. If not set by the user, this is the same as the profile path.- Parameters:
profile- the profile to use as a base- Returns:
- User's files directory as a String
- See Also:
-
setUserFilesPath
Set the user's files directory.- Parameters:
profile- The profile to use as a basepath- The path to the user's files directory- See Also:
-
getProfilePath
Get the profile directory. Uses the Profile returned byProfileManager.getActiveProfile()as a base. If that is null, gets the preferences path.- Returns:
- Profile directory
- See Also:
-
getProfilePath
Get the profile directory. If the profile is null or has a null directory, this is the same as the preferences path.- Parameters:
profile- the profile to use as a base- Returns:
- Profile directory
- 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.
- See Also:
- On Microsoft Windows systems, this is
-
getProgramPath
Get the JMRI program directory. If the program directory has not been previously sets, first sets the program directory to the value specified in the Java System propertyjmri.path.program, or.if that property is not set.- 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
-
findExternalFilename
Get the URL of a portable filename if it can be located usingfindURL(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 static InputStream findInputStream(@Nonnull String path, 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:
-
getUserResourcePath
Get the resources directory within the user's files directory.- Returns:
- path to [user's file]/resources/
-
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 static 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 thePROGRAMdirectory and JARs in the class pathFileUtil.Location.USERlimits the search to thePREFERENCES,PROFILE, andSETTINGSdirectories (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.TODO: add @CheckForNull annotation / fix Possible null pointers.
- 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 static 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 thePROGRAMdirectory and JARs in the class pathFileUtil.Location.USERlimits the search to thePROFILEdirectory
- 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
-
jmriJarFile
Get the JMRI distribution jar file.- Returns:
- the JAR file containing the JMRI library or null if not running from a JAR file
-
logFilePaths
Log all paths at the INFO level. -
getScriptsPath
Get the path to the scripts directory using the Profile returned byProfileManager.getActiveProfile()as the base.- Returns:
- the scriptsPath
-
getScriptsPath
Get the path to the scripts directory.- Parameters:
profile- the Profile to use as the base- Returns:
- the scriptsPath
-
setScriptsPath
Set the path to python scripts.- Parameters:
profile- the profile to set the path forpath- the scriptsPath to set
-
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. Would be significantly simpler with Java 7. File is assumed to be encoded using UTF-8- 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- See Also:
-
rotate
public static void rotate(@Nonnull File file, int max, @CheckForNull String extension) throws IOException 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:
-
getDefault
Get the default instance of FileUtilSupport.- Returns:
- the default instance of FileUtilSupport
-