Class FileUtilitiesKt
-
- All Implemented Interfaces:
public final class FileUtilitiesKt
-
-
Method Summary
Modifier and Type Method Description final static BooleandeleteQuietly(File $self)Try and delete this file or directory quietly. final static Doublesize(File $self)Acquire the size of this file or directory in bytes. final static List<File>regexWalk(File $self, List<Regex> filters, FileWalkDirection direction)Walk this directory and return all file-objects which match any of the regular expressions in the provided list. final static List<File>filteredWalk(File $self, List<String> filters, FilterType filterType, FileWalkDirection direction, Boolean recursive)Walk this directory and return all file-objects which match the specified filter-type using the passed list of filters. final static Unitcreate(File $self, Boolean createFileOrDir, Boolean asDirectory)All parent directories are created, but not the file itself. final static UnitwriteAtomically(File $self, File temporaryFile, Function1<File, Unit> write)Produce this file's new contents through write so that a reader can only ever see the whole of them. final static UnitwriteTextAtomically(File $self, String text, File temporaryFile)Replace this file's contents with text, completely or not at all — writeAtomically for a string. final static StringwithLineEndings(String $self, LineEnding lineEnding)This text with every line terminated by lineEnding, whichever convention it arrived in. final static BooleantestFileWrite(File $self)Test whether files can be written to this file denoting a directory, by creating a throwaway file in it and removing it again. -
-
Method Detail
-
deleteQuietly
final static Boolean deleteQuietly(File $self)
Try and delete this file or directory quietly. Deleting something quietly means that
No guarantee is made whether the file or directory is successfully deleted
No exceptions are thrown if an error occurs
No information is carried outside should an exception occur, meaning you have no information about why the deletion, if it failed
- Returns:
trueif, and only if, the file or directory was deleted.
-
size
final static Double size(File $self)
Acquire the size of this file or directory in bytes. If this file-object denotes a directory, then the size of all files in the directory will be checked and the sum of them returned.
- Returns:
The filesize of this file, or sum of sizes of all files in this directory.
-
regexWalk
final static List<File> regexWalk(File $self, List<Regex> filters, FileWalkDirection direction)
Walk this directory and return all file-objects which match any of the regular expressions in the provided list.
- Parameters:
filters- List of regular expressions to use for filtering.direction- The direction in which to walk the directory.- Returns:
All files inside this directory which matched the given filters.
-
filteredWalk
final static List<File> filteredWalk(File $self, List<String> filters, FilterType filterType, FileWalkDirection direction, Boolean recursive)
Walk this directory and return all file-objects which match the specified filter-type using the passed list of filters.
- Parameters:
filters- List of Strings to use for filteringfilterType- Whether to filter by FilterType.CONTAINS (default), FilterType.ENDS_WITH or FilterType.STARTS_WITH.direction- The direction in which to walk the directory.- Returns:
All files inside this directory which matched the given filters.
-
create
final static Unit create(File $self, Boolean createFileOrDir, Boolean asDirectory)
All parent directories are created, but not the file itself.
createFileOrDir in combination with asDirectory will result in this file being treated as a directory. As such, all parents will be created, along with this file itself being created as a directory.
createFileOrDir without asDirectory will result in this file being created as a file.
- Parameters:
createFileOrDir- Whether the file or directory should be created.asDirectory- true to create a directory, false to create a file.
-
writeAtomically
final static Unit writeAtomically(File $self, File temporaryFile, Function1<File, Unit> write)
Produce this file's new contents through write so that a reader can only ever see the whole of them.
write is handed temporaryFile — a sibling, so the move below stays within one filesystem — and that file is then moved onto this one with StandardCopyOption.ATOMIC_MOVE, which is a rename on every platform ServerPackCreator runs on. Content that is not finished therefore leaves the previous contents in place instead of a half-file, which writing in place cannot promise: a killed process or a full disk leaves exactly as many bytes as it managed, and that partial file satisfies every
isFilecheck afterwards.The default staging name is unique per call, not derived from the destination alone: two writers sharing one would interleave their bytes into it and then move the mixture into place, which is the corruption this exists to prevent. Given one each, both stage a complete copy and the last move wins.
temporaryFile is a parameter purely so the failure can be exercised — provoking a partial write is not something a test can do, but pointing the staging somewhere unusable reaches the same question.
- Parameters:
temporaryFile- Where the new contents are staged before being moved into place.write- Fills the staging file.
-
writeTextAtomically
final static Unit writeTextAtomically(File $self, String text, File temporaryFile)
Replace this file's contents with text, completely or not at all — writeAtomically for a string.
- Parameters:
text- The contents this file should hold once the write has completed.temporaryFile- Where the contents are staged before being moved into place.
-
withLineEndings
final static String withLineEndings(String $self, LineEnding lineEnding)
This text with every line terminated by lineEnding, whichever convention it arrived in.
Normalises to LF before expanding, so a carriage return is only ever replaced and never deleted: deleting it reads as a conversion while a file using lone carriage returns — a classic-Mac editor, or a half-finished conversion elsewhere — loses every line separator it had and becomes one long line that is still syntactically plausible. Normalising first is also what keeps LineEnding.CRLF from doubling the carriage returns of text that already had them.
- Parameters:
lineEnding- The convention the returned text uses throughout.
-
testFileWrite
final static Boolean testFileWrite(File $self)
Test whether files can be written to this file denoting a directory, by creating a throwaway file in it and removing it again. The probe file's name is generated per call, never fixed: a shared name is defeated both by anything already holding it and by a second probe deleting this one's file mid-check — measured at 34 of 64 concurrent probes reporting a writable directory as unwritable — and callers act on a false answer by refusing a directory or, in
ApiProperties, by refusing to start. If this file is not a directory, an IllegalArgumentException will be thrown.
-
-
-
-