mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 16:47:30 +02:00
práctica 3, falta la última parte
This commit is contained in:
22
es/um/redes/nanoFiles/util/FileNameUtil.java
Normal file
22
es/um/redes/nanoFiles/util/FileNameUtil.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package es.um.redes.nanoFiles.util;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class FileNameUtil {
|
||||
|
||||
/**
|
||||
* Devuelve una ruta disponible a partir de un nombre base. Si ya existe,
|
||||
* añade sufijos .1, .2, etc. hasta encontrar un nombre libre.
|
||||
*/
|
||||
public static Path chooseAvailableName(String baseName) {
|
||||
Path path = Paths.get(baseName);
|
||||
int suffix = 1;
|
||||
while (Files.exists(path)) {
|
||||
path = Paths.get(baseName + "." + suffix);
|
||||
suffix++;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user