File tree 1 file changed +21
-3
lines changed
common/src/main/java/eu/cloudnetservice/common/io
1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -241,14 +241,32 @@ public static void delete(@Nullable Path path) {
241
241
}
242
242
243
243
/**
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())}
246
247
*
247
248
* @return the path to the temporary file.
249
+ * @see #createTempFile(String)
248
250
*/
249
251
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 ) {
250
263
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 ;
252
270
}
253
271
254
272
/**
You can’t perform that action at this time.
0 commit comments