Skip to content

Commit 7d30bc8

Browse files
committed
more sophisticated temporary file api
1 parent f00c6c8 commit 7d30bc8

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

common/src/main/java/eu/cloudnetservice/common/io/FileUtil.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,32 @@ public static void delete(@Nullable Path path) {
241241
}
242242

243243
/**
244-
* Resolves a random path in the temp directory of the cloud and creates all needed parents directories for a
245-
* temporary file including the {@link FileUtil#TEMP_DIR}.
244+
* Resolves a random path in the temp directory of the cloud and creates all needed parent directories for a temporary
245+
* file including the {@link FileUtil#TEMP_DIR}. This method is equivalent to
246+
* {@code FileUtil.createTempFile(UUID.randomUUID().toString())}
246247
*
247248
* @return the path to the temporary file.
249+
* @see #createTempFile(String)
248250
*/
249251
public static @NonNull Path createTempFile() {
252+
return createTempFile(UUID.randomUUID().toString());
253+
}
254+
255+
/**
256+
* Resolves a path in the temp directory of the cloud and creates all needed parent directories for a temporary file
257+
* including the {@link FileUtil#TEMP_DIR}. The final name might not be the argument if the file already exists.
258+
*
259+
* @param name the preferred name of the temporary file.
260+
* @return the path to the temporary file.
261+
*/
262+
public static @NonNull Path createTempFile(String name) {
250263
createDirectory(TEMP_DIR);
251-
return TEMP_DIR.resolve(UUID.randomUUID().toString());
264+
int id = 0;
265+
Path path;
266+
do {
267+
path = TEMP_DIR.resolve(id++ == 0 ? name : name + "-" + id);
268+
} while (Files.exists(path));
269+
return path;
252270
}
253271

254272
/**

0 commit comments

Comments
 (0)