commit | author | age
|
e369b9
|
1 |
/* |
c45b52
|
2 |
fm - File management class library |
e369b9
|
3 |
Copyright (C) 2024 Ulrich Hilger |
U |
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.fm; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.IOException; |
|
22 |
import java.nio.file.Files; |
|
23 |
|
|
24 |
/** |
|
25 |
* |
|
26 |
* @author Ulrich Hilger |
|
27 |
* @version 0.1, 08.11.2024 |
|
28 |
*/ |
|
29 |
public class Duplicator { |
|
30 |
|
|
31 |
public String duplizieren(String base, String relPfad) throws IOException { |
|
32 |
File srcFile = new File(base, relPfad); |
|
33 |
String fnameext = srcFile.getName(); |
|
34 |
int dotpos = fnameext.lastIndexOf("."); |
|
35 |
String fname = fnameext.substring(0, dotpos); |
|
36 |
String ext = fnameext.substring(dotpos); |
|
37 |
File srcDir = srcFile.getParentFile(); |
|
38 |
File destFile = new File(srcDir, fname + "-Kopie" + ext); |
|
39 |
int i = 1; |
|
40 |
while (destFile.exists()) { |
|
41 |
destFile = new File(srcDir, fname + "-Kopie-" + Integer.toString(++i) + ext); |
|
42 |
} |
|
43 |
Files.copy(srcFile.toPath(), destFile.toPath()); |
|
44 |
return destFile.getName(); |
|
45 |
} |
|
46 |
|
|
47 |
} |