/* fm - File management class library Copyright (C) 2024 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.fm; import java.io.File; import java.io.IOException; import java.nio.file.Files; /** * * @author Ulrich Hilger * @version 0.1, 08.11.2024 */ public class Duplicator { public String duplizieren(String base, String relPfad) throws IOException { File srcFile = new File(base, relPfad); String fnameext = srcFile.getName(); int dotpos = fnameext.lastIndexOf("."); String fname = fnameext.substring(0, dotpos); String ext = fnameext.substring(dotpos); File srcDir = srcFile.getParentFile(); File destFile = new File(srcDir, fname + "-Kopie" + ext); int i = 1; while (destFile.exists()) { destFile = new File(srcDir, fname + "-Kopie-" + Integer.toString(++i) + ext); } Files.copy(srcFile.toPath(), destFile.toPath()); return destFile.getName(); } }