Add FileAccess#move and FileAccess#copy methods

This commit is contained in:
JeremyStar™ 2024-12-02 21:39:28 +01:00
parent de6b5f76f2
commit a7dabfb923
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -628,42 +628,7 @@ public final class FileAccess {
} }
// -----> File/Directory creation and deletion // -----> File/Directory creation, moving, copying and deletion
/**
* Deletes the file.
* If it doesn't exist, nothing will be done.
*
* @return this instance
* @since v1-alpha8
*/
public @NotNull FileAccess delete() {
if (exists()) {
Logger.diag("Deleting \"" + path + "\"");
//noinspection ResultOfMethodCallIgnored
file.delete();
}
return this;
}
/**
* Marks this file for deletion at engine shutdown.
*
* @return this instance
* @see Engine#shutdown()
* @see Engine#shutdown(int)
* @since v1-alpha8
*/
public @NotNull FileAccess deleteOnShutdown() {
Logger.diag("Marking \"" + path + "\" for deletion at engine shutdown");
// Append path to scheduledDeletion array
List<@NotNull Path> scheduledDeletionList = new ArrayList<>(Arrays.stream(scheduledDeletion).toList());
scheduledDeletionList.add(path);
scheduledDeletion = scheduledDeletionList.toArray(new Path[0]);
return this;
}
/** /**
* Creates the file. * Creates the file.
* If it already exists, nothing will be done. * If it already exists, nothing will be done.
@ -725,6 +690,67 @@ public final class FileAccess {
return this; return this;
} }
/**
* Moves this file.
*
* @param destination destination to move this file to
* @return supplied destination
* @throws IOException on an IO error
* @since v1-alpha9
*/
public @NotNull FileAccess move(@NotNull FileAccess destination) throws IOException {
Files.move(path, destination.path);
return destination;
}
/**
* Copies this file.
*
* @param destination destination to copy this file to
* @return this instance
* @throws IOException on an IO error
* @since v1-alpha9
*/
public @NotNull FileAccess copy(@NotNull FileAccess destination) throws IOException {
Files.move(path, destination.path);
return this;
}
/**
* Deletes this file.
* If it doesn't exist, nothing will be done.
*
* @return this instance
* @since v1-alpha8
*/
public @NotNull FileAccess delete() {
if (exists()) {
Logger.diag("Deleting \"" + path + "\"");
//noinspection ResultOfMethodCallIgnored
file.delete();
}
return this;
}
/**
* Marks this file for deletion at engine shutdown.
*
* @return this instance
* @see Engine#shutdown()
* @see Engine#shutdown(int)
* @since v1-alpha8
*/
public @NotNull FileAccess deleteOnShutdown() {
Logger.diag("Marking \"" + path + "\" for deletion at engine shutdown");
// Append path to scheduledDeletion array
List<@NotNull Path> scheduledDeletionList = new ArrayList<>(Arrays.stream(scheduledDeletion).toList());
scheduledDeletionList.add(path);
scheduledDeletion = scheduledDeletionList.toArray(new Path[0]);
return this;
}
// -----> File locking // -----> File locking
/** /**