-
Notifications
You must be signed in to change notification settings - Fork 1
/
Terminal.class
203 lines (168 loc) · 5.8 KB
/
Terminal.class
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
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.company;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardCopyOption;
public class Terminal {
String curpath = System.getProperty("user.dir");
Terminal() {
}
public void cd(String s) throws IOException {
File file;
if (s.charAt(0) == 'C') {
file = new File(s);
} else {
file = new File(this.curpath + "/" + s);
}
if (file.exists()) {
this.curpath = file.getCanonicalPath();
} else {
throw new NoSuchFileException(file.getCanonicalPath(), (String)null, "does not exist.");
}
}
public void mv(String source, String destination) throws IOException, NoSuchFileException {
if (source.charAt(0) != 'C') {
source = this.curpath + "\\" + source;
}
if (destination.charAt(0) != 'C') {
destination = this.curpath + "\\" + destination;
}
File src = new File(source);
File dst = new File(destination);
if (!src.exists()) {
throw new NoSuchFileException(src.getAbsolutePath(), (String)null, "does not exist.");
} else if (dst.isFile()) {
throw new IOException("Can't move into file.");
} else {
if (!dst.exists()) {
Files.move(src.toPath(), src.toPath().resolveSibling(dst.getName()));
} else {
Files.move(src.toPath(), dst.toPath().resolve(src.toPath().getFileName()), StandardCopyOption.REPLACE_EXISTING);
}
}
}
public void mkdir(String filename) throws IOException {
if (filename.charAt(0) != 'C') {
filename = this.curpath + "\\" + filename;
}
File f1 = new File(filename);
boolean state = f1.mkdir();
if (f1.exists()) {
if (state) {
System.out.print("Directory is Created in " + f1.getAbsolutePath());
} else {
throw new IOException("Couldn't create Directory");
}
} else {
throw new IOException("Directory" + f1.getAbsolutePath() + "doesn't exist");
}
}
public void rmdir(String filename) throws NoSuchFileException {
if (filename.charAt(0) != 'C') {
filename = this.curpath + "\\" + filename;
}
File f1 = new File(filename);
if (!f1.exists()) {
throw new NoSuchFileException(f1.getAbsolutePath(), (String)null, "doesn't exist");
} else {
if (f1.isDirectory()) {
if (f1.list().length == 0) {
f1.delete();
System.out.println("Directory is deleted Successfully");
} else {
System.out.println("Couldn't delete the directory because it's not empty");
}
} else {
System.out.println("couldn't delete it because This is a file not a directory");
}
}
}
public void clear() {
for(int i = 0; i < 1000; ++i) {
System.out.println("\n");
}
}
public void pwd() {
System.out.println(this.curpath);
}
public String echo(String s) {
return s;
}
public void ls() {
File f1 = new File(this.curpath);
String[] show = f1.list();
if (show != null) {
String[] var3 = show;
int var4 = show.length;
for(int var5 = 0; var5 < var4; ++var5) {
String x = var3[var5];
System.out.println(x);
}
}
}
public void cat(String file) {
if (file.charAt(0) != 'C') {
file = this.curpath + "\\" + file;
}
File f1 = new File(file);
String line = "";
String S = "";
if (f1.exists()) {
try {
FileReader filereader = new FileReader(file);
BufferedReader bufferReader;
for(bufferReader = new BufferedReader(filereader); (line = bufferReader.readLine()) != null; S = S + line) {
}
bufferReader.close();
System.out.println(S);
} catch (IOException var7) {
System.out.println("An Error has occurred ");
}
} else {
System.out.println("Couldn't find file");
}
}
public void rm(String s) {
if (s.charAt(0) != 'C') {
s = this.curpath + "\\" + s;
}
File f1 = new File(s);
if (!f1.exists()) {
System.out.println("Couldn't find file");
} else if (f1.isDirectory()) {
System.out.println("Couldn't delete it");
} else if (!f1.delete()) {
System.out.println("An error has occurred");
} else {
f1.delete();
System.out.println("couldn't delete it because This is a file not a directory");
}
}
public void cp(String source, String dest) throws IOException {
if (source.charAt(0) != 'C') {
source = this.curpath + "\\" + source;
}
if (dest.charAt(0) != 'C') {
dest = this.curpath + "\\" + dest;
}
File f1 = new File(source);
File f2 = new File(dest);
if (!f1.exists()) {
System.out.println("DOESN'T EXIST");
}
if (!f2.exists()) {
if (f2.isDirectory()) {
System.out.println("DOESN'T EXIST");
}
} else {
Files.copy(f1.toPath(), f2.toPath().resolve(f1.toPath().getFileName()), StandardCopyOption.REPLACE_EXISTING);
}
}
}