-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListFiles.java
More file actions
316 lines (294 loc) · 7.2 KB
/
ListFiles.java
File metadata and controls
316 lines (294 loc) · 7.2 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import java.io.*;
import java.util.*;
import java.lang.*;
public class ListFiles
{
ArrayList<FileInfo> list;
public ListFiles()
{
this.list = new ArrayList<FileInfo>();
}
public static void main(String[] args)
{
/*try
{
toList.sizeSort(args[1]);
}
catch(IOException e)
{
System.err.printf("Error: %s%n",e.getMessage());
System.exit(1);
}*/
ListFiles toList = new ListFiles();
if(args[0].equals("-size"))
{
if(args[1].equals("-gather"))
{
String[] dirs = new String[args.length-2];
for(int i=0;i<dirs.length;i++)
{
dirs[i] = args[2+i];
}
try
{
toList.sizeSort(dirs);
}
catch(IOException e)
{
System.err.printf("Error: %s%n",e.getMessage());
System.exit(1);
}
}
else
{
String[] dirs = new String[args.length-1];
for(int i=0;i<dirs.length;i++)
{
dirs[i] = args[1+i];
}
try
{
for(int i=0;i<dirs.length;i++)
{
toList.sizeSort(dirs[i]);
toList.list = new ArrayList<FileInfo>();
System.out.println("-----------------");
}
}
catch(IOException e)
{
System.err.printf("Error: %s%n",e.getMessage());
System.exit(1);
}
}
}
else if(args[0].equals("-gather"))
{
String[] dirs = new String[args.length-1];
for(int i=0;i<dirs.length;i++)
{
dirs[i] = args[1+i];
}
System.out.println(args.length);
try
{
toList.nameSort(dirs);
}
catch(IOException e)
{
System.err.printf("Error: %s%n",e.getMessage());
System.exit(1);
}
}
else
{
String[] dirs = new String[args.length];
System.out.println(args.length);
for(int i=0;i<dirs.length;i++)
{
dirs[i] = args[i];
}
try
{
for(int i=0;i<dirs.length;i++)
{
toList.nameSort(dirs[i]);
toList.list = new ArrayList<FileInfo>();
}
}
catch(IOException e)
{
System.err.printf("Error: %s%n",e.getMessage());
System.exit(1);
}
}
}
public void nameSort(String dir) throws IOException
{
File[] fileList = listFiles(new File(dir));
for(int i=0;i<fileList.length;i++)
{
if(fileList[i].isFile())
{
this.list.add(new FileInfo(fileList[i].getName(),fileList[i].length()));
}
}
Collections.sort(this.list);
for(int i=0;i<this.list.size();i++)
{
System.out.println(this.list.get(i));
}
}
public void nameSort(String[] dirs) throws IOException
{
for(int i=0;i<dirs.length;i++)
{
File[] files = listFiles(new File(dirs[i]));
for(int z=0;z<files.length;z++)
{
if(files[z].isFile())
{
this.list.add(new FileInfo(files[z].getName(),files[z].length()));
}
}
}
Collections.sort(this.list);
for(int i=0;i<this.list.size();i++)
{
System.out.println(this.list.get(i));
}
}
public void sizeSort(String dir) throws IOException
{
File[] fileList = listFiles(new File(dir));
for(int i=0;i<fileList.length;i++)
{
if(fileList[i].isFile())
{
this.list.add(new FileInfo(fileList[i].getName(),fileList[i].length()));
}
}
Collections.sort(this.list);
for(int i=0;i<this.list.size();i++)
{
long max = this.list.get(i).getFileSize();
int maxIndex = i;
for(int j=i+1;j<this.list.size();j++)
{
if(this.list.get(j).getFileSize()>max)
{
max = this.list.get(j).getFileSize();
maxIndex = j;
}
}
long currLong = this.list.get(i).getFileSize();
this.list.get(i).setFileSize(max);
this.list.get(maxIndex).setFileSize(currLong);
String currString = this.list.get(i).getFileName();
this.list.get(i).setFileName(this.list.get(maxIndex).getFileName());
this.list.get(maxIndex).setFileName(currString);
}
for(int i=0;i<this.list.size();i++)
{
System.out.println(this.list.get(i));
}
}
public void sizeSort(String[] dirs) throws IOException
{
for(int i=0;i<dirs.length;i++)
{
File[] files = listFiles(new File(dirs[i]));
for(int z=0;z<files.length;z++)
{
if(files[z].isFile())
{
this.list.add(new FileInfo(files[z].getName(),files[z].length()));
}
}
}
Collections.sort(this.list);
for(int i=0;i<this.list.size();i++)
{
long max = this.list.get(i).getFileSize();
int maxIndex = i;
for(int j=i+1;j<this.list.size();j++)
{
if(this.list.get(j).getFileSize()>max)
{
max = this.list.get(j).getFileSize();
maxIndex = j;
}
}
long currLong = this.list.get(i).getFileSize();
this.list.get(i).setFileSize(max);
this.list.get(maxIndex).setFileSize(currLong);
String currString = this.list.get(i).getFileName();
this.list.get(i).setFileName(this.list.get(maxIndex).getFileName());
this.list.get(maxIndex).setFileName(currString);
}
for(int i=0;i<this.list.size();i++)
{
System.out.println(this.list.get(i));
}
}
private String[] listFileNames(File directory) throws IOException
{
if(!directory.exists()||!directory.isDirectory())
{
throw new IOException(directory + "is not a directory");
}
else
{
String[] fileNames = directory.list();
return fileNames;
}
}
private File[] listFiles(File directory) throws IOException
{
if(!directory.exists()||!directory.isDirectory())
{
throw new IOException(directory + "is not a directory");
}
else
{
File[] files = directory.listFiles();
return files;
}
}
private int[] numSort(int[] unsort)
{
for(int i=0;i<unsort.length;i++)
{
int min = unsort[i];
int minIndex = i;
for(int j=i;j<unsort.length;j++)
{
if(unsort[j]<min)
{
min = unsort[j];
minIndex = j;
}
}
int curr = unsort[i];
unsort[i] = min;
unsort[minIndex] = curr;
}
return unsort;
}
private class FileInfo implements Comparable<FileInfo>
{
private String fileName;
private long fileSize;
public FileInfo(String name, long size)
{
this.fileName = name;
this.fileSize = size;
}
public String getFileName()
{
return this.fileName;
}
public long getFileSize()
{
return this.fileSize;
}
public void setFileName(String newName)
{
this.fileName = newName;
}
public void setFileSize(long newSize)
{
this.fileSize = newSize;
}
public String toString()
{
String output = String.format("%12d %s", this.fileSize, this.fileName);
return output;
}
@Override
public int compareTo(FileInfo that)
{
return this.getFileName().compareTo(that.getFileName());
}
}
}