public class Os
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Os.DeletionResult |
static class |
Os.TmpDirFinder
utility for finding a usable (writable) tmp dir, preferring java.io.tmpdir
(unless it's weird, e.g.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
LINE_SEPARATOR |
Constructor and Description |
---|
Os() |
Modifier and Type | Method and Description |
---|---|
static void |
deleteOnExit(java.io.File directoryToDeleteIfEmptyOrFile)
deletes the given file or empty directory on exit
|
static int |
deleteOnExitEmptyParentsUpTo(java.io.File subsubDirOrFile,
java.io.File dir)
causes empty directories from subsubdir up to and including dir to be deleted on exit;
useful e.g.
|
static void |
deleteOnExitRecursively(java.io.File directoryToCleanOrFile)
deletes the given file or directory and, in the case of directories, any contents;
similar to apache commons FileUtils.cleanDirectoryOnExit but corrects a bug in that implementation
which causes it to fail if content is added to that directory after the hook is registered
|
static void |
deleteOnExitRecursivelyAndEmptyParentsUpTo(java.io.File directoryToCleanOrFile,
java.io.File highestAncestorToDelete)
like
deleteOnExitRecursively(File) followed by deleteOnExitEmptyParentsUpTo(File, File) |
static Os.DeletionResult |
deleteRecursively(java.io.File dir)
tries to delete a directory recursively;
use with care - see http://stackoverflow.com/questions/8320376/why-is-files-deletedirectorycontents-deprecated-in-guava.
|
static Os.DeletionResult |
deleteRecursively(java.io.File dir,
boolean skipSafetyChecks)
as
deleteRecursively(File) but includes safety checks to prevent deletion of / or ~
or anything else not passing checkSafe(File) , unless the skipSafetyChecks parameter is set |
static Os.DeletionResult |
deleteRecursively(java.lang.String dir) |
static java.lang.String |
fromHome(java.lang.String path) |
static java.lang.String |
home()
user's home directory
|
static boolean |
isAbsolutish(java.lang.String path)
Checks whether a file system path is absolute in a platform neutral way.
|
static boolean |
isMicrosoftWindows() |
static java.lang.String |
mergePaths(java.lang.String... items)
merges paths using forward slash as the "local OS file separator", because it is recognised on windows,
making paths more consistent and avoiding problems with backslashes being escaped.
|
static java.lang.String |
mergePathsUnix(java.lang.String... items)
merges paths using forward slash (unix way);
now identical to
mergePaths(String...) but kept for contexts
where caller wants to indicate the target system should definitely be unix |
static java.io.File |
mkdirs(java.io.File dir)
as
File.mkdirs() but throwing on failure and returning the directory made for fluent convenience |
static java.lang.String |
nativePath(java.lang.String path) |
static java.io.File |
newTempDir(java.lang.Class<?> clazz)
as
newTempDir(String) , using the class as the basis for a prefix |
static java.io.File |
newTempDir(java.lang.String prefix)
creates a temp dir which will be deleted on exit
|
static java.io.File |
newTempFile(java.lang.Class<?> clazz,
java.lang.String ext)
as
newTempFile(String, String) using the class as the basis for a prefix |
static java.io.File |
newTempFile(java.lang.String prefix,
java.lang.String ext)
creates a private temp file which will be deleted on exit;
either prefix or ext may be null;
if ext is non-empty and not > 4 chars and not starting with a ., then a dot will be inserted;
if either name part is too long it will be shortened to prevent filesystem errors
|
static java.lang.String |
tidyPath(java.lang.String path)
Tidy up a file path.
|
static java.lang.String |
tmp()
returns the best tmp dir to use; see
Os.TmpDirFinder for the logic
(and the explanation why this is needed!) |
static java.lang.String |
user()
user name
|
static java.io.File |
writePropertiesToTempFile(java.util.Properties props,
java.io.File tempDir,
java.lang.String prefix,
java.lang.String suffix) |
static java.io.File |
writePropertiesToTempFile(java.util.Properties props,
java.lang.String prefix,
java.lang.String suffix) |
static java.io.File |
writeToTempFile(java.io.InputStream is,
java.io.File tempDir,
java.lang.String prefix,
java.lang.String suffix)
writes given contents to a temporary file which will be deleted on exit, located under the given directory
|
static java.io.File |
writeToTempFile(java.io.InputStream is,
java.lang.String prefix,
java.lang.String suffix)
writes given contents to a temporary file which will be deleted on exit
|
public static java.lang.String tmp()
Os.TmpDirFinder
for the logic
(and the explanation why this is needed!)public static java.lang.String user()
public static java.lang.String home()
public static java.lang.String mergePathsUnix(java.lang.String... items)
mergePaths(String...)
but kept for contexts
where caller wants to indicate the target system should definitely be unixpublic static java.lang.String mergePaths(java.lang.String... items)
public static Os.DeletionResult deleteRecursively(java.io.File dir)
also note this implementation refuses to delete / or ~ or anything else not passing checkSafe(File)
.
if you might really want to delete something like that, use deleteRecursively(File, boolean)
.
public static Os.DeletionResult deleteRecursively(java.io.File dir, boolean skipSafetyChecks)
deleteRecursively(File)
but includes safety checks to prevent deletion of / or ~
or anything else not passing checkSafe(File)
, unless the skipSafetyChecks parameter is setpublic static Os.DeletionResult deleteRecursively(java.lang.String dir)
#deleteRecursively(File)}
public static void deleteOnExit(java.io.File directoryToDeleteIfEmptyOrFile)
similar to File.deleteOnExit()
except it is smart about trying to delete longer filenames first
(and the shutdown hook order does not use proprietary java hooks)
note this does not delete non-empty directories; see deleteOnExitRecursively(File)
for that
public static void deleteOnExitRecursively(java.io.File directoryToCleanOrFile)
public static int deleteOnExitEmptyParentsUpTo(java.io.File subsubDirOrFile, java.io.File dir)
returns number of directories queued for deletion so caller can check for errors if desired; if dir is not an ancestor of subsubdir this logs a warning but does not throw
public static void deleteOnExitRecursivelyAndEmptyParentsUpTo(java.io.File directoryToCleanOrFile, java.io.File highestAncestorToDelete)
deleteOnExitRecursively(File)
followed by deleteOnExitEmptyParentsUpTo(File, File)
public static java.io.File mkdirs(java.io.File dir)
File.mkdirs()
but throwing on failure and returning the directory made for fluent conveniencepublic static java.io.File writeToTempFile(java.io.InputStream is, java.lang.String prefix, java.lang.String suffix)
public static java.io.File writeToTempFile(java.io.InputStream is, java.io.File tempDir, java.lang.String prefix, java.lang.String suffix)
public static java.io.File writePropertiesToTempFile(java.util.Properties props, java.lang.String prefix, java.lang.String suffix)
public static java.io.File writePropertiesToTempFile(java.util.Properties props, java.io.File tempDir, java.lang.String prefix, java.lang.String suffix)
public static java.lang.String tidyPath(java.lang.String path)
Removes duplicate or trailing path separators (Unix style forward
slashes only), replaces initial ~ with the
value of home()
and folds out use of .. and
. path segments.
com.google.common.io.Files#simplifyPath(String)
public static boolean isAbsolutish(java.lang.String path)
As a consequence of the platform neutrality some edge cases are not handled correctly:
path
- A string representing a file system path.public static java.lang.String fromHome(java.lang.String path)
public static java.lang.String nativePath(java.lang.String path)
public static boolean isMicrosoftWindows()
public static java.io.File newTempFile(java.lang.String prefix, java.lang.String ext)
public static java.io.File newTempFile(java.lang.Class<?> clazz, java.lang.String ext)
newTempFile(String, String)
using the class as the basis for a prefixpublic static java.io.File newTempDir(java.lang.String prefix)
public static java.io.File newTempDir(java.lang.Class<?> clazz)
newTempDir(String)
, using the class as the basis for a prefix