Skip to content

Commit 5c30246

Browse files
committed
ListFiles.java
1 parent a409860 commit 5c30246

File tree

1 file changed

+316
-0
lines changed

1 file changed

+316
-0
lines changed

ListFiles.java

Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.lang.*;
4+
public class ListFiles
5+
{
6+
ArrayList<FileInfo> list;
7+
public ListFiles()
8+
{
9+
this.list = new ArrayList<FileInfo>();
10+
}
11+
public static void main(String[] args)
12+
{
13+
/*try
14+
{
15+
toList.sizeSort(args[1]);
16+
}
17+
catch(IOException e)
18+
{
19+
System.err.printf("Error: %s%n",e.getMessage());
20+
System.exit(1);
21+
}*/
22+
ListFiles toList = new ListFiles();
23+
24+
if(args[0].equals("-size"))
25+
{
26+
if(args[1].equals("-gather"))
27+
{
28+
String[] dirs = new String[args.length-2];
29+
for(int i=0;i<dirs.length;i++)
30+
{
31+
dirs[i] = args[2+i];
32+
}
33+
try
34+
{
35+
toList.sizeSort(dirs);
36+
}
37+
catch(IOException e)
38+
{
39+
System.err.printf("Error: %s%n",e.getMessage());
40+
System.exit(1);
41+
}
42+
}
43+
else
44+
{
45+
String[] dirs = new String[args.length-1];
46+
for(int i=0;i<dirs.length;i++)
47+
{
48+
dirs[i] = args[1+i];
49+
}
50+
try
51+
{
52+
for(int i=0;i<dirs.length;i++)
53+
{
54+
toList.sizeSort(dirs[i]);
55+
toList.list = new ArrayList<FileInfo>();
56+
System.out.println("-----------------");
57+
}
58+
}
59+
catch(IOException e)
60+
{
61+
System.err.printf("Error: %s%n",e.getMessage());
62+
System.exit(1);
63+
}
64+
65+
}
66+
}
67+
else if(args[0].equals("-gather"))
68+
{
69+
String[] dirs = new String[args.length-1];
70+
for(int i=0;i<dirs.length;i++)
71+
{
72+
dirs[i] = args[1+i];
73+
}
74+
System.out.println(args.length);
75+
76+
try
77+
{
78+
79+
toList.nameSort(dirs);
80+
81+
}
82+
catch(IOException e)
83+
{
84+
System.err.printf("Error: %s%n",e.getMessage());
85+
System.exit(1);
86+
}
87+
88+
}
89+
else
90+
{
91+
String[] dirs = new String[args.length];
92+
System.out.println(args.length);
93+
for(int i=0;i<dirs.length;i++)
94+
{
95+
dirs[i] = args[i];
96+
}
97+
try
98+
{
99+
for(int i=0;i<dirs.length;i++)
100+
{
101+
toList.nameSort(dirs[i]);
102+
toList.list = new ArrayList<FileInfo>();
103+
}
104+
}
105+
catch(IOException e)
106+
{
107+
System.err.printf("Error: %s%n",e.getMessage());
108+
System.exit(1);
109+
}
110+
111+
}
112+
113+
}
114+
public void nameSort(String dir) throws IOException
115+
{
116+
File[] fileList = listFiles(new File(dir));
117+
for(int i=0;i<fileList.length;i++)
118+
{
119+
if(fileList[i].isFile())
120+
{
121+
this.list.add(new FileInfo(fileList[i].getName(),fileList[i].length()));
122+
}
123+
}
124+
Collections.sort(this.list);
125+
for(int i=0;i<this.list.size();i++)
126+
{
127+
System.out.println(this.list.get(i));
128+
}
129+
130+
}
131+
132+
public void nameSort(String[] dirs) throws IOException
133+
{
134+
for(int i=0;i<dirs.length;i++)
135+
{
136+
File[] files = listFiles(new File(dirs[i]));
137+
for(int z=0;z<files.length;z++)
138+
{
139+
if(files[z].isFile())
140+
{
141+
this.list.add(new FileInfo(files[z].getName(),files[z].length()));
142+
}
143+
}
144+
}
145+
Collections.sort(this.list);
146+
for(int i=0;i<this.list.size();i++)
147+
{
148+
System.out.println(this.list.get(i));
149+
}
150+
}
151+
public void sizeSort(String dir) throws IOException
152+
{
153+
File[] fileList = listFiles(new File(dir));
154+
for(int i=0;i<fileList.length;i++)
155+
{
156+
if(fileList[i].isFile())
157+
{
158+
this.list.add(new FileInfo(fileList[i].getName(),fileList[i].length()));
159+
}
160+
}
161+
Collections.sort(this.list);
162+
for(int i=0;i<this.list.size();i++)
163+
{
164+
long max = this.list.get(i).getFileSize();
165+
int maxIndex = i;
166+
for(int j=i+1;j<this.list.size();j++)
167+
{
168+
if(this.list.get(j).getFileSize()>max)
169+
{
170+
max = this.list.get(j).getFileSize();
171+
maxIndex = j;
172+
}
173+
}
174+
long currLong = this.list.get(i).getFileSize();
175+
this.list.get(i).setFileSize(max);
176+
this.list.get(maxIndex).setFileSize(currLong);
177+
String currString = this.list.get(i).getFileName();
178+
this.list.get(i).setFileName(this.list.get(maxIndex).getFileName());
179+
this.list.get(maxIndex).setFileName(currString);
180+
}
181+
for(int i=0;i<this.list.size();i++)
182+
{
183+
System.out.println(this.list.get(i));
184+
}
185+
186+
}
187+
188+
public void sizeSort(String[] dirs) throws IOException
189+
{
190+
for(int i=0;i<dirs.length;i++)
191+
{
192+
File[] files = listFiles(new File(dirs[i]));
193+
for(int z=0;z<files.length;z++)
194+
{
195+
if(files[z].isFile())
196+
{
197+
this.list.add(new FileInfo(files[z].getName(),files[z].length()));
198+
}
199+
}
200+
}
201+
Collections.sort(this.list);
202+
for(int i=0;i<this.list.size();i++)
203+
{
204+
long max = this.list.get(i).getFileSize();
205+
int maxIndex = i;
206+
for(int j=i+1;j<this.list.size();j++)
207+
{
208+
if(this.list.get(j).getFileSize()>max)
209+
{
210+
max = this.list.get(j).getFileSize();
211+
maxIndex = j;
212+
}
213+
}
214+
long currLong = this.list.get(i).getFileSize();
215+
this.list.get(i).setFileSize(max);
216+
this.list.get(maxIndex).setFileSize(currLong);
217+
String currString = this.list.get(i).getFileName();
218+
this.list.get(i).setFileName(this.list.get(maxIndex).getFileName());
219+
this.list.get(maxIndex).setFileName(currString);
220+
}
221+
for(int i=0;i<this.list.size();i++)
222+
{
223+
System.out.println(this.list.get(i));
224+
}
225+
226+
}
227+
228+
private String[] listFileNames(File directory) throws IOException
229+
{
230+
if(!directory.exists()||!directory.isDirectory())
231+
{
232+
throw new IOException(directory + "is not a directory");
233+
}
234+
else
235+
{
236+
String[] fileNames = directory.list();
237+
return fileNames;
238+
}
239+
}
240+
241+
private File[] listFiles(File directory) throws IOException
242+
{
243+
if(!directory.exists()||!directory.isDirectory())
244+
{
245+
throw new IOException(directory + "is not a directory");
246+
}
247+
else
248+
{
249+
File[] files = directory.listFiles();
250+
return files;
251+
}
252+
}
253+
254+
255+
256+
private int[] numSort(int[] unsort)
257+
{
258+
for(int i=0;i<unsort.length;i++)
259+
{
260+
int min = unsort[i];
261+
int minIndex = i;
262+
for(int j=i;j<unsort.length;j++)
263+
{
264+
if(unsort[j]<min)
265+
{
266+
min = unsort[j];
267+
minIndex = j;
268+
}
269+
}
270+
int curr = unsort[i];
271+
unsort[i] = min;
272+
unsort[minIndex] = curr;
273+
}
274+
return unsort;
275+
}
276+
277+
278+
private class FileInfo implements Comparable<FileInfo>
279+
{
280+
private String fileName;
281+
private long fileSize;
282+
public FileInfo(String name, long size)
283+
{
284+
this.fileName = name;
285+
this.fileSize = size;
286+
}
287+
public String getFileName()
288+
{
289+
return this.fileName;
290+
}
291+
public long getFileSize()
292+
{
293+
return this.fileSize;
294+
}
295+
public void setFileName(String newName)
296+
{
297+
this.fileName = newName;
298+
}
299+
public void setFileSize(long newSize)
300+
{
301+
this.fileSize = newSize;
302+
}
303+
public String toString()
304+
{
305+
String output = String.format("%12d %s", this.fileSize, this.fileName);
306+
return output;
307+
}
308+
@Override
309+
public int compareTo(FileInfo that)
310+
{
311+
return this.getFileName().compareTo(that.getFileName());
312+
}
313+
}
314+
}
315+
316+

0 commit comments

Comments
 (0)