forked from ZipCodeCore/JavaFileManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.java
More file actions
108 lines (98 loc) · 3.62 KB
/
FileManager.java
File metadata and controls
108 lines (98 loc) · 3.62 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
import java.io.*;
public class FileManager {
private Console console;
private FileOperator fileops;
private boolean running = true;
public FileManager(Console c) {
this.console = c;
this.fileops = new FileOperator(c);
}
public static void main(String[] args) {
FileManager app = new FileManager(new Console());
int status = app.handleUserInput();
java.lang.System.exit(status);
}
public Integer handleUserInput() {
this.sendIntro();
String message = "Enter a desired command: ";
while (this.running) {
String str = console.promptForString(message);
if (null == str || str.equals("")) {
console.sendMessage("Did nothing, as you commanded.");
} else if("help".equals(str)){
this.sendIntro();
} else {
this.processCommand(str);
}
}
console.sendMessage("You're exiting File Manager.");
return 0; // normal exit
}
private void processCommand(String command) {
switch (command) {
case "list":
fileops.list(console.promptForString("Enter folder's path: "));
break;
case "info":
fileops.info(console.promptForString("Enter desired file/folder's path: "));
break;
case "mkdir":
fileops.createDir(console.promptForString("Enter new folder's path: "));
break;
case "rename":
this.renameFile(command);
break;
case "copy":
case "move":
this.performCopyMove(command);
break;
case "delete":
fileops.delete(console.promptForString("Enter path of file/folder you want to delete: "));
break;
case "quit":
running = false;
break;
default:
console.sendMessage("Command you entered, doesn't exist! (enter HELP for available commands)");
break;
}
}
private void renameFile(String str) {
String s1 = console.promptForString("Enter path of file/folder you want to rename: ");
File f = new File(s1);
if(!f.exists()){
console.sendMessage("File doesn't exists!");
return;
}
String s2 = console.promptForString("Enter new name of file/folder: ");
fileops.rename(s1, s2);
}
private void performCopyMove(String command) {
String s1 = console.promptForString("Enter path of file/folder you would like to copy/move: ");
String s2 = console.promptForString("Enter destination path: ");
File f1 = new File(s1);
File f2 = new File(s2);
if(f1.isDirectory()){
try{
fileops.copyCutDir(f1, f2, command);
if("move".equals(command)){
console.sendMessage("Moving is successfuly finished.");
} else {
console.sendMessage("Copying is successfuly finished.");
}
} catch(Exception e) {
if("move".equals(command)){
console.sendMessage("Moving failed!");
} else {
console.sendMessage("Copying failed!");
}
}
} else {
fileops.copyCut(f1, f2, command);
}
}
private void sendIntro() {
String commands = "Available commands: \nlist, info\nmkdir, rename\ncopy, move, delete\nquit\n";
console.sendMessage(commands);
}
}