Class FileUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic PathcreateDirectories(Path path) static booleanfileExists(String filePathString) static booleanisPathAChildOfParent(Path parent, Path potentialChild) Checks whether a child path falls under a particular parentstatic StringRewritespathso every path separator matches the current filesystem's separator.static FileresolvePath(File base, String path) Resolves a path relative a base directory.static voidCopied from Lucene's FSDirectory.fsync(String)
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
resolvePath
Resolves a path relative a base directory.This method does what "new File(base,path)" Should do, if it wasn't completely lame: If path is absolute, then a File for that path is returned; if it's not absolute, then a File is returned using "path" as a child of "base")
-
copyFile
- Throws:
IOException
-
sync
Copied from Lucene's FSDirectory.fsync(String)- Parameters:
fullFile- the File to be synced to disk- Throws:
IOException- if the file could not be synced
-
fileExists
-
createDirectories
- Throws:
IOException
-
isPathAChildOfParent
Checks whether a child path falls under a particular parentUseful for validating user-provided relative paths, which generally aren't expected to "escape" a given parent/root directory. Parent and child paths are "normalized" by
Path.normalize(). This removes explicit backtracking (e.g. "../") though it will not resolve symlinks if any are present in the provided Paths, so some forms of parent "escape" remain undetected. Paths needn't exist as a file or directory for comparison purposes.Note, this method does not consult the file system
- Parameters:
parent- the path of a 'parent' node. Path must be syntactically valid but needn't exist.potentialChild- the path of a potential child. Typically obtained via: parent.resolve(relativeChildPath). Path must be syntactically valid but needn't exist.- Returns:
- true if 'potentialChild' nests under the provided 'parent', false otherwise.
-
normalizeToOsPathSeparator
Rewritespathso every path separator matches the current filesystem's separator.Both
/and\are replaced withFileSystems.getDefault()'s separator. This is useful before iteratingPathcomponents, since on POSIX a\is otherwise part of a name rather than a separator, so\..\conf\xwould be a single component.Caveat: on non-Windows platforms this reinterprets any filename that legitimately contains a backslash as a path with additional components. Callers that treat backslash as a valid filename character on POSIX must not use this method.
- Parameters:
path- the path to normalize- Returns:
- path normalized with the filesystem's default separator, or
nullifpathwasnull
-