1 // Copy.java
2
3 import java.io.IOException;
4
5 import java.nio.file.Files;
6 import java.nio.file.Paths;
7 import java.nio.file.Path;
8 import java.nio.file.InvalidPathException;
9 import java.nio.file.FileAlreadyExistsException;
10
11 /** 12 * Copy kopiert eine Datei. 13 * @author H.Drachenfels 14 * @version 10.1.2019 15 */
16 public final class Copy {
17 private Copy() { }
18
19 /** 20 * Legt eine Zieldatei (Name args[0]) 21 * als Kopie einer Quelldatei (Name args[1]) an. 22 * @param args Quelldatei und Zieldatei 23 */
24 public static void main(String[] args) {
25
26 if (args.length != 2) {
27 System.err.println("Aufruf: java Copy Quelldatei Zieldatei");
28 System.exit(1);
29 }
30
31 try {
32 // Quelle pruefen
33 Path quelle = Paths.get(args[0]);
34
35 if (Files.notExists(quelle)) {
36 System.err.printf("Quelle existiert nicht: %s%n", quelle);
37 System.exit(1);
38 }
39
40 if (!Files.isRegularFile(quelle)) {
41 System.err.printf("Quelle ist keine normale Datei: %s%n",
42 quelle);
43 System.exit(1);
44 }
45
46 if (!Files.isReadable(quelle)) {
47 System.err.printf("Quelle ist nicht lesbar: %s%n", quelle);
48 System.exit(1);
49 }
50
51 // Kopieren
52 Path ziel = Paths.get(args[1]);
53 Files.copy(quelle, ziel);
54
55 } catch (InvalidPathException x) {
56 System.err.printf("Ungueltiger Dateiname: %s%n", x.getMessage());
57 System.exit(1);
58 } catch (FileAlreadyExistsException x) {
59 System.err.printf("Ziel existiert bereits: %s%n", x.getMessage());
60 System.exit(1);
61 } catch (IOException x) {
62 System.err.printf("Kopierfehler: %s%n", x.getMessage());
63 System.exit(1);
64 }
65 }
66 }
67