public class FileMisc extends Object
Miscellaneous utilties for copying files around.
Modifier and Type | Field and Description |
---|---|
static String |
PROTOCOL
The
file:// protocol. |
Constructor and Description |
---|
FileMisc() |
Modifier and Type | Method and Description |
---|---|
static void |
assertMacApp(File file)
Ensures that the given file ends with “.app” on macOS, does nothing on all others.
|
static String |
asUrl(File file)
Prefixes
file:// to the file’s absolute path. |
static void |
cleanDir(File dirToRemove)
Deletes the given file or directory if it exists, then creates a fresh directory in its place.
|
static void |
concat(Iterable<File> toMerge,
File dst)
Concats the first files and writes them to the last file.
|
static boolean |
containsExecutablePermission(Set<PosixFilePermission> permissions)
Returns true if any of the bits contain the executable permission.
|
static void |
copyFile(File srcFile,
File dstFile,
String... toReplace)
Copies from src to dst and performs a simple copy-replace templating operation along the way.
|
static void |
flatten(File dirToRemove)
Flattens a single directory (moves its children to be its peers, then deletes the given directory.
|
static void |
forceDelete(File f)
Calls
FileUtils.forceDelete(File) and throws an exception if it fails. |
static boolean |
hasToken(File dir,
String name)
Returns true iff the given directory has a file with the given name.
|
static boolean |
hasToken(File dir,
String name,
String content)
Returns true iff the given directory has a file with the given name containing the given content.
|
static boolean |
hasTokenFile(File tokenFile)
Returns true iff the given directory has a file with the given name.
|
static boolean |
hasTokenFile(File tokenFile,
String content)
Returns true iff the given directory has a file with the given name containing the given content.
|
static List<File> |
list(File d)
Lists the children of the given file in a safe way (
File.listFiles() can return null). |
static String |
macApp()
Returns “.app” on macOS, and empty string on all others.
|
static String |
macContentsEclipse()
Returns “Contents/Eclipse/” on macOS, and empty string on all others.
|
static void |
mkdirs(File d)
Calls
File.mkdirs() and throws an exception if it fails. |
static void |
modifyFile(File file,
Function<String,String> modifier)
Modifies the given file in place.
|
static String |
noQuote(String input)
Throws an exception if the given input property contains whitespace.
|
static List<File> |
parseListFile(Project project,
List<Object> inputs) |
static String |
quote(File input)
Quotes the absolute path of the given file iff it contains whitespace.
|
static String |
quote(String input)
Quotes the given input string iff it contains whitespace.
|
static Optional<String> |
readToken(File dir,
String name)
Returns the contents of a file with the given name, if it exists.
|
static String |
toOctalFileMode(Set<PosixFilePermission> permissions)
Converts a set of
PosixFilePermission to chmod-style octal file mode. |
static int |
toOctalFileModeInt(Set<PosixFilePermission> permissions)
Converts a set of
PosixFilePermission to chmod-style octal file mode. |
static String |
toUnixNewline(String input)
Enforces unix newlines on the given string.
|
static void |
writeToken(File dir,
String name)
Writes an empty file with the given name in the given directory.
|
static void |
writeToken(File dir,
String name,
String value)
Writes a file with the given name, to the given directory, containing the given value.
|
static void |
writeTokenFile(File tokenFile,
String value)
Writes a token file containing the given value.
|
public static final String PROTOCOL
The file://
protocol.
public static List<File> list(File d)
Lists the children of the given file in a safe way (File.listFiles()
can return null).
public static void mkdirs(File d)
Calls File.mkdirs()
and throws an exception if it fails.
public static void forceDelete(File f)
Calls FileUtils.forceDelete(File)
and throws an exception if it fails. If the file doesn’t exist at all, that’s fine.
public static String toUnixNewline(String input)
Enforces unix newlines on the given string.
public static String quote(String input)
Quotes the given input string iff it contains whitespace.
public static String quote(File input)
Quotes the absolute path of the given file iff it contains whitespace.
public static String noQuote(String input)
Throws an exception if the given input property contains whitespace.
public static void writeToken(File dir, String name, String value) throws IOException
Writes a file with the given name, to the given directory, containing the given value.
IOException
public static void writeTokenFile(File tokenFile, String value) throws IOException
Writes a token file containing the given value.
IOException
public static Optional<String> readToken(File dir, String name) throws IOException
Returns the contents of a file with the given name, if it exists.
IOException
public static void writeToken(File dir, String name) throws IOException
Writes an empty file with the given name in the given directory.
IOException
public static boolean hasToken(File dir, String name) throws IOException
Returns true iff the given directory has a file with the given name.
IOException
public static boolean hasToken(File dir, String name, String content) throws IOException
Returns true iff the given directory has a file with the given name containing the given content.
IOException
public static boolean hasTokenFile(File tokenFile) throws IOException
Returns true iff the given directory has a file with the given name.
IOException
public static boolean hasTokenFile(File tokenFile, String content) throws IOException
Returns true iff the given directory has a file with the given name containing the given content.
IOException
public static void copyFile(File srcFile, File dstFile, String... toReplace) throws IOException
Copies from src to dst and performs a simple copy-replace templating operation along the way.
copyFile(src, dst,
"%username%", "lskywalker"
"%firstname%", "Luke",
"%lastname%", "Skywalker");
IOException
public static void modifyFile(File file, Function<String,String> modifier) throws IOException
Modifies the given file in place.
IOException
public static void cleanDir(File dirToRemove) throws IOException
Deletes the given file or directory if it exists, then creates a fresh directory in its place.
IOException
public static void flatten(File dirToRemove) throws IOException
Flattens a single directory (moves its children to be its peers, then deletes the given directory.
before:
root/
toFlatten/
child1
child2
flatten("root/toFlatten")
after:
root/
child1
child2
IOException
public static void concat(Iterable<File> toMerge, File dst) throws IOException
Concats the first files and writes them to the last file.
IOException
public static int toOctalFileModeInt(Set<PosixFilePermission> permissions)
Converts a set of PosixFilePermission
to chmod-style octal file mode.
public static String toOctalFileMode(Set<PosixFilePermission> permissions)
Converts a set of PosixFilePermission
to chmod-style octal file mode.
public static boolean containsExecutablePermission(Set<PosixFilePermission> permissions)
Returns true if any of the bits contain the executable permission.
public static String macApp()
Returns “.app” on macOS, and empty string on all others.
public static String macContentsEclipse()
Returns “Contents/Eclipse/” on macOS, and empty string on all others.
public static void assertMacApp(File file)
Ensures that the given file ends with “.app” on macOS, does nothing on all others.