forked from ZipCodeCore/JavaFileManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperator.java
More file actions
191 lines (177 loc) · 6.12 KB
/
FileOperator.java
File metadata and controls
191 lines (177 loc) · 6.12 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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class FileOperator {
private Console cons;
private char[] illegalchars = {92, 47, 58, 63, 42, 34, 60, 62, 124};
private String illegalcharsstring = "\\ / : ? * \" < > |";
public FileOperator(Console con) {
this.cons = con;
}
public void list(String path){
File path1 = new File(path);
if(path1.exists() && path1.isDirectory()){
String[] lista = path1.list();
if(lista.length == 0){
cons.sendMessage("Folder is empty.");
} else {
for (String s : lista) {
cons.sendMessage(s);
}
}
} else {
cons.sendMessage("Wrong path entered!");
}
}
public void info(String path){
File path1 = new File(path);
if(path1.exists()){
cons.sendMessage("Name: " + path1.getName());
cons.sendMessage("Absolute path: " + path1.getAbsolutePath());
cons.sendMessage("Relative path: " + path1.getPath());
cons.sendMessage("Size: " + path1.length());
Path p = Paths.get(path);
try {
BasicFileAttributes bfa = Files.readAttributes(p, BasicFileAttributes.class);
cons.sendMessage("Created: " + time(bfa.creationTime().toMillis()));
} catch (IOException ex) {
cons.sendMessage(ex.toString());
}
cons.sendMessage("Last Modified: " + time(path1.lastModified()));
} else {
cons.sendMessage("Wrong path entered!");
}
}
public String time(long l){
Instant instant = Instant.ofEpochMilli(l);
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd. MMMM yyyy. HH:mm:ss");
return dateTime.format(dateTimeFormatter);
}
public void createDir(String path){
File folder = new File(path);
String s = folder.getName();
boolean illegalChar = checkForIllegalChars(s);
try {
if(!folder.exists()){
if(illegalChar){
cons.sendMessage("A file name can't contain any of the following characters: ");
cons.sendMessage(illegalcharsstring);
} else {
makeDir(folder.getParentFile().exists(), folder);
cons.sendMessage("Created a folder called " + folder.getName());
}
} else {
cons.sendMessage("Folder called " + folder.getName() + " already exists.");
}
} catch (Exception e) {
cons.sendMessage("Couldn't create a folder called " + folder.getName());
}
}
private boolean checkForIllegalChars(String s) {
for (char kh : illegalchars) {
if (s.indexOf(kh) >= 0) {
return true;
}
}
return false;
}
public void rename(String of, String nf) {
File oldFile = new File(of);
String[] strings = of.split("\\\\+");
int n = strings.length;
strings[n-1] = nf;
StringBuilder sb = new StringBuilder();
sb.append(strings[0]);
sb.append("\\");
for(int i = 1; i <strings.length; i++){
sb.append("\\");
sb.append(strings[i]);
}
File newFile = new File(sb.toString());
if(newFile.exists()){
cons.sendMessage("File with desired name already exists!");
return;
}
if(oldFile.renameTo(newFile)){
cons.sendMessage("Rename succesful.");
} else {
cons.sendMessage("Rename failed!");
}
}
public void copyCut(File f1, File f2, String c){
try (FileInputStream inStream = new FileInputStream(f1);
FileOutputStream outStream = new FileOutputStream(f1);) {
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
if("move".equals(c)){
f1.delete();
cons.sendMessage("Moving is successfuly finished.");
} else {
cons.sendMessage("Copying is successfuly finished.");
}
} catch (IOException e) {
if("move".equals(c)){
cons.sendMessage("Moving failed!");
} else {
cons.sendMessage("Copying failed!");
}
}
}
public void delete(String path){
File file = new File(path);
if(file.exists()){
if(file.isFile()){
file.delete();
cons.sendMessage("File successfully deleted!");
} else {
deleteDir(file);
cons.sendMessage("Folder successfully deleted!");
}
} else {
cons.sendMessage("Cannot delete " + file.getName() + " because " + file.getName() + " does not exist on this path.");
}
}
public void deleteDir(File f){
File[] files = f.listFiles();
if(files != null){
for(File f1 : files){
deleteDir(f1);
}
}
f.delete();
}
public void copyCutDir(File f1, File f2, String c) {
if(!f2.exists()){
f2.mkdir();
}
String fs[] = f1.list();
for (String f : fs) {
copyCutDir(new File(f1, f), new File(f2, f), c);
}
if("move".equals(c)){
deleteDir(f1);
}
}
public void makeDir(boolean b, File f){
if(b){
f.mkdir();
} else {
f.mkdirs();
}
}
}