Class FileUtils
- Version:
- $Revision$ $Date$
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcomputeHashForDirectory(Path targetDirectory) static StringcomputeHashForFile(Path targetFile) static FilenameFiltercreateExtensionFilenameFilter(String extension) Creates a file filter which only lets files through which have the given extension.static StringcreateValidFilename(String name) Creates a valid filename for the given source name.static booleandeleteTree(File tree) Recursively deletes the directorytree.static FileensureExtension(File file, String extension) Returns a file with the given extension.static StringensureExtension(String path, String extension) Returns a file with the given extension.static URIensureJarURI(URI uri) Ensures that the given URI is a path into a jar file.static FileexchangeExtension(File file, String extension) Returns a file with the given new extension.static StringexchangeExtension(String path, String extension) Returns the file string with the given new extension.static StringgetDisplayText(File file, int maxLength) static StringgetExtension(File file) Gets the extension (which always includes a leading dot) of a file.static StringgetExtension(String path) Gets the extension of a file path.static intgetExtensionDotPos(String path) static URLgetFileAsUrl(File file) Gets a normalized URL representation for the given file.static StringgetFilenameFromPath(String path) Retrieves the file name from a complete path.static StringgetFilenameWithoutAnyExtensions(String filename) Removes all extensions from a fiven file namestatic StringGets the filename without its extension from the given file path.static StringgetFilenameWithoutExtension(String filename) Gets the filename without its extension from the given filename.static PathgetPathFromURI(URI uri) static URIgetRelativeUri(URI rootURI, File file) static FilegetUrlAsFile(URL url) Inverse ofgetFileAsUrl(java.io.File).static String[]listFilePathsWithExtension(File dir, String extension) Lists the filenames with the specified extension contained in the given directory.static File[]listFilesWithExtension(File dir, String extension) Lists the files with the specified extension contained in the given directory.static Stringstatic Stringstatic FileConverts the given path object into a file object.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
getExtension
Gets the extension (which always includes a leading dot) of a file.- Parameters:
file- the file whose extension is to be extracted.- Returns:
- the extension string which always includes a leading dot. Returns
nullif the file has no extension.
-
getExtension
Gets the extension of a file path.- Parameters:
path- the file path whose extension is to be extracted.- Returns:
- the extension string which always includes a leading dot. Returns
nullif the file path has no extension.
-
getFilenameWithoutExtension
Gets the filename without its extension from the given file path.- Parameters:
file- the file whose filename is to be extracted.- Returns:
- the filename without its extension.
-
getFilenameWithoutExtension
Gets the filename without its extension from the given filename.- Parameters:
filename- the name of the file whose filename is to be extracted.- Returns:
- the filename without its extension.
-
getFilenameWithoutAnyExtensions
Removes all extensions from a fiven file name- Parameters:
filename- the name of the file whose filename is to be extracted.- Returns:
- the filename without its extensions.
-
exchangeExtension
Returns the file string with the given new extension. If the given file string have no extension, the given extension will be added.Example1:
"tie.point.grids\tpg1.hdr"
results to"tie.point.grids\tpg1.raw"
Example2:
"tie.point.grids\tpg1"
results to"tie.point.grids\tpg1.raw"
- Parameters:
path- the string to change the extensionextension- the new file extension including a leading dot (e.g.".raw").- Throws:
IllegalArgumentException- if one of the given strings are null or empty.
-
exchangeExtension
Returns a file with the given new extension. If the given file have no extension, the given extension will be added.Example1:
"tie.point.grids\tpg1.hdr"
results to"tie.point.grids\tpg1.raw"
Example2:
"tie.point.grids\tpg1"
results to"tie.point.grids\tpg1.raw"
- Parameters:
file- the file to change the extensionextension- the new file extension including a leading dot (e.g.".raw").- Throws:
IllegalArgumentException- if one of the parameter strings are null or empty.
-
ensureExtension
Returns a file with the given extension. If the given path have no extension or the extension are not equal to the given extension, the given extension will be added.Example1: param path = example.dim param extension = ".dim"
"example.dim"
results to"example.dim"
Example2: param path = example param extension = ".dim"
"example"
results to"example.dim"
Example3: param path = example.lem param extension = ".dim"
"example.lem"
results to"example.lem.dim"
- Parameters:
path- the string to ensure the extensionextension- the new file extension including a leading dot (e.g.".raw").- Throws:
IllegalArgumentException- if one of the given strings are null or empty.
-
ensureExtension
Returns a file with the given extension. If the given file has no extension or the extension is not equal to the given extension, the given extension will be added.Example1: param file = example.dim param extension = ".dim"
"example.dim"
results to"example.dim"
Example2: param file = example param extension = ".dim"
"example"
results to"example.dim"
Example3: param file = example.lem param extension = ".dim"
"example.lem"
results to"example.lem.dim"
- Parameters:
file- the file to ensure the extensionextension- the new file extension including a leading dot (e.g.".raw").- Throws:
IllegalArgumentException- if one of the parameter strings are null or empty.
-
getExtensionDotPos
-
getFilenameFromPath
Retrieves the file name from a complete path. example: "c:/testData/MERIS/meris_test.N1" will be converted to "meris_test.N1" -
listFilesWithExtension
Lists the files with the specified extension contained in the given directory.- See Also:
-
listFilePathsWithExtension
Lists the filenames with the specified extension contained in the given directory.- See Also:
-
createExtensionFilenameFilter
Creates a file filter which only lets files through which have the given extension.- See Also:
-
createValidFilename
Creates a valid filename for the given source name. The method returns a string which is the given name where each occurence of a character which is not a letter, a digit or one of '_', '-', '.' is replaced by an underscore. The returned string always has the same length as the source name.- Parameters:
name- the source name, must not benull
-
getFileAsUrl
Gets a normalized URL representation for the given file.Unlike the
File.toURL()method, this method automatically escapes characters that are illegal in URLs. It converts the given abstract pathname into a URL by first converting it into a URI, via theFile.toURI()method, and then converting the URI into a URL via theURI.toURLmethod. .See also the 'Usage Note' of the
File.toURL()API documentation.- Parameters:
file- the file- Returns:
- a normalized URL representation
- Throws:
MalformedURLException
-
getUrlAsFile
Inverse ofgetFileAsUrl(java.io.File).- Parameters:
url- the URL- Returns:
- the file
- Throws:
URISyntaxException
-
getDisplayText
-
readText
- Throws:
IOException
-
readText
- Throws:
IOException
-
deleteTree
Recursively deletes the directorytree.- Parameters:
tree- directory to be deleted- Returns:
trueif and only if the file or directory is successfully deleted;falseotherwise
-
getRelativeUri
-
getPathFromURI
- Parameters:
uri- TheURIto create thePathfrom.- Returns:
- The converted
Path. - Throws:
IOException- If thePathcould not be createdIllegalArgumentException- IfURIis not valid
-
toFile
Converts the given path object into a file object. If opposite toPath.toFile(), the method unwraps paths that have a "jar" URI.- Parameters:
path- The path.- Returns:
- The file object.
-
ensureJarURI
Ensures that the given URI is a path into a jar file. If the given URI points to a file and ends with '.jar' and starts with 'file:' the given URI is changed. 'jar:' is prepended and '!/' appended to the uri.- Parameters:
uri- the uri which shall be corrected- Returns:
- the corrected URI
- Throws:
IOException- If the URI could not be converted into aPath
-
computeHashForFile
- Throws:
IOException
-
computeHashForDirectory
- Throws:
IOException
-