-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.java
More file actions
298 lines (213 loc) · 10.1 KB
/
Copy pathFileManager.java
File metadata and controls
298 lines (213 loc) · 10.1 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class FileManager {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Command Line input
public static void main(String[] args) {
boolean check = false;
while (!check) { // Program will run unless interrupted
System.out.println("This program is able to group files based on their type into a chosen folder.");
System.out.println("Start with typing in the path of a folder or hard drive (E.g. C:\\).\n");
File folder = null;
File[] directory;
while (folder == null) { // Check for null folder
System.out.print("Enter the path to the folder: ");
folder = getFolder(getString()); // Getting folder at given path
}
FolderWork folderWorkObject = new FolderWork(folder);
StructureWork structureWorkObject = new StructureWork(folder);
folderWorkObject.printFolder();
directory = structureWorkObject.getStructure(); // Getting the skeleton of files from a folder
check = getType(directory, folder); // Catalogue browsing
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
}
}
private static Boolean getType(File[] directory, File folder) { // Gets an element from folder and detects its type
int index;
File file;
System.out.print("Enter the index of a file/folder: ");
index = getInteger();
while (index < -1 || index > directory.length) { // Getting index for the file/folder the user wants to operate.
System.out.print("Invalid index. Enter the index of a file/folder: ");
index = getInteger();
}
// Index at 0 returns current folder File
// Index at -1 return parent folder File (with protection)
if (index == 0) file = folder;
else if (index == -1) {
if (!(folder.getParent() == null))
file = folder.getParentFile();
else {
file = folder;
}
} else file = directory[index - 1]; // else we get a File at index
System.out.printf("The element number %d: '%s' chosen. Proceeding...", index, file.getPath());
// The number of operations if element is a folder:
if (file.isDirectory()) {
return folderWork(file);
} else if (file.isFile()) {
return fileWork(file);
} else return true;
}
private static boolean fileWork(File file) { // Method for file operations
FileWork fileWorkObject = new FileWork(file);
System.out.println("Choose an option: \nA. delete\nB. open\nElse to exit the program.");
// User IO
System.out.print("Enter the choice: ");
String choice = getString();
switch (choice.toLowerCase()) {
case ("a"): {
System.out.println("Are you sure you want to delete file " + file.getPath() + " (y/n)");
choice = getString();
if ("y".equals(choice.toLowerCase())) {
if (fileWorkObject.deleteFile())
System.out.println("Deletion of file: " + file.getName() + " is successful");
else System.out.println("File: " + file.getName() + " is not deleted.");
return false;
}
System.out.println("Operation aborted.");
return false;
}
case ("b"): {
fileWorkObject.openFile();
return false;
}
default: {
return true;
}
}
}
private static boolean folderWork(File folder) { // Method for folder operations
System.out.println("Choose an option: \nA. Open\nB. Classify Files\nC. Auto Classify\nElse to exit the program.\n");
FolderWork folderWorkObject = new FolderWork(folder);
StructureWork structureWorkObject = new StructureWork(folder);
// User IO
String choice = getString();
switch (choice.toLowerCase()) {
case ("a"): {
folderWorkObject.printFolder();
getType(structureWorkObject.getStructure(), folder);
break;
}
case ("b"): {
folderWorkObject.printFolder();
File[] directory = structureWorkObject.getStructure();
structureWorkObject.printTypeHash();
HashMap<String, ArrayList<File>> typeFileMap = structureWorkObject.getTypeFileMap();
TypeWork typeWork = new TypeWork(typeFileMap, br);
ArrayList<File> oneTypeFiles = typeWork.getOneTypeFiles();
if (oneTypeFiles == null) {
System.out.println("No files to typeHash...");
return false;
// Making sure we break out of the loop if no files can be scanned
} else {
CopyWork copyWorkObject = new CopyWork(oneTypeFiles);
File resultTypeFolder = copyWorkObject.getResultTypeFolder();
if (resultTypeFolder == null) return true; // Stop algorithm if the folder is not created.
System.out.println("New destination is obtained.");
for (File each : oneTypeFiles) // For each file in the array of files obtained
// the advantage of looped method call is that each File is treated individually
// therefore if an exception happens for one file, it will not affect other
moveFiles(each, resultTypeFolder);
return false; // false -> continue working
}
}
case "c": {
File[] directory = structureWorkObject.getStructure();
structureWorkObject.printTypeHash();
HashMap<String, ArrayList<File>> typeFileMap = structureWorkObject.getTypeFileMap();
structureWorkObject.printTypeHash();
//printTypeHash(typeFileMap); // prints out the typeFileMap
for (Map.Entry<String, ArrayList<File>> entrySet : typeFileMap.entrySet()) {
String type = entrySet.getKey();
ArrayList<File> files = entrySet.getValue();
File resultTypeFolder = autoNewFolder(type, files.get(0));
for (File each : files) {
System.out.printf("Moving %s to %s\n", each.toString(), resultTypeFolder.toString());
moveFiles(each, resultTypeFolder);
}
}
}
default: { // If user enters any other choice stop the program
System.out.println("Chosen non-existing option. Closing...");
return true;
}
}
return true;
}
private static void moveFiles(File file, File folder) { // "moves" a file from file location to folder location
try {
File destFile = new File(folder.getPath() + "\\" + file.getName()); // Creating a File at the destination to write into
// it will be the same type and name as the original one
System.out.println("Copying " + file.getName() + "...");
Files.copy(file.toPath(), destFile.toPath());
System.out.println("Successful.");
if (deleteFile(file)) { // Deleting old file to complete moving operation
// notice that deletion happens only if Files.copy had not thrown an exception
System.out.println("Old file deleted.");
} else System.out.println("No rights to delete the file."); // Most likely cause of the exception
} catch (FileAlreadyExistsException e) {
System.out.println("File already exists at a given location..."); // Very likely to pop up
} catch (Exception e) {
System.out.println("Copying failed bruh... " + e);
}
}
private static File autoNewFolder(String type, File path) {
path = path.getParentFile();
try {
Path kek = Files.createDirectories(new File(path.getPath() + "\\" + "dump" + type.toUpperCase()).toPath());
return kek.toFile();
} catch (Exception e) {
System.out.printf("Creation of the auto folder for type %s failed.", type);
}
return null;
}
private static boolean deleteFile(File file) { // Deletion of a file
try {
return file.delete();
} catch (Exception e) {
System.out.println("Something went wrong with deleting file " + file.getName());
return false;
}
}
private static File getFolder(String path) { // Returns the File only ifDirectory()
try {
File file = new File(path);
if (file.isDirectory())
return file;
else {
System.out.println("The chosen path does not lead to a directory. Please try again.");
return null;
}
} catch (Exception e) {
System.out.println("The path does not exist.");
return null;
}
}
private static String getString() { // Getter for String values
try {
return br.readLine();
} catch (Exception e) {
System.out.println("Wrong data entered, please try again.");
return getString();
}
}
private static int getInteger() { // Getter for int values.
try {
return Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Wrong data entered, please try again.");
return getInteger();
}
}
}
// Name: Mikhail Semyanovskiy
// Project: File Manager
// Title: File Manager
// Date: 6/5/2019