-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyTool.java
More file actions
116 lines (104 loc) · 3.64 KB
/
CopyTool.java
File metadata and controls
116 lines (104 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package module6.exceptions_and_io.filecopierapp;
import java.io.*;
import java.util.Scanner;
import java.util.StringJoiner;
/**
* Assignment 6 - File I/O
* Demonstration: {@link https://youtu.be/olEf6f1j9bM}
*
* @author Mae Morella
*/
public class CopyTool {
private static Scanner scanner = new Scanner(System.in);
public static File askFile(String promptMessage) {
String path = "";
while (path.length() < 1) {
System.out.print(promptMessage + " ");
path = scanner.nextLine();
}
if (path.equalsIgnoreCase(":q")) {
exit();
return null;
} else {
return new File(path);
}
}
public static File askSourceFile() {
while(true) {
File file = askFile("Enter path to source file (ex. \"./textfile.txt\"):");
if (!file.canRead()) {
System.out.printf("Error: Could not read file at '%s' (Enter ':q' to quit)\n", file.getAbsolutePath());
continue;
} else {
return file;
}
}
}
public static File askDestinationFile() {
while (true) {
File file = askFile(("Enter path to destination (ex. \"./textfile2.txt\"):"));
if (file.exists() && !file.canWrite()) {
System.out.printf("Error: File at '%s' is not writable\n", file.getAbsolutePath());
continue;
} else {
return file;
}
}
}
public static void printFile(File file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file)) ) {
int lineNumber = 0;
System.out.printf("=== %s ===\n", file.getName());
while (reader.ready()) {
String line = reader.readLine();
lineNumber++;
System.out.printf("%-3d: %s\n", lineNumber, line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static String readStringFromFile(File file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
StringJoiner s = new StringJoiner("\n");
while (reader.ready()) {
String line = reader.readLine();
s.add(line);
}
return s.toString();
} catch (IOException e) {
System.out.printf("Error occured reading file at '%s'\n", file.getAbsolutePath());
}
return null;
}
public static void writeStringToFile(String str, File file) {
try (PrintWriter writer = new PrintWriter(file)) {
writer.print(str);
System.out.printf("Successfully wrote to %s\n", file.getName());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public static void exit() {
System.out.println("Exiting...");
System.exit(0);
}
public static void main(String[] args) {
System.out.println("\n");
while(true) {
File source = askSourceFile();
printFile(source);
System.out.println();
File destination = askDestinationFile();
if (destination.exists()) {
System.out.printf("'%s' already exists. Overwrite? (y/N): ", destination.getName());
boolean overwrite = scanner.nextLine().equalsIgnoreCase("y");
if (!overwrite) {
continue;
}
}
writeStringToFile(readStringFromFile(source), destination);
System.out.println();
}
}
}