-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
67 lines (59 loc) · 1.61 KB
/
main.java
File metadata and controls
67 lines (59 loc) · 1.61 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
import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* Created by candy on 16-4-30.
*/
public class main {
public static void main(String arg[]) {
try {
Option option = new Option();
option.readdata();
option.Sortbyfirst();
for (int m = 0;m<option.i;m++){
System.out.println(option.arrayList.get(m));
}
option.outputdata();
} catch (Exception e) {
}
}
}
class Option {
ArrayList<String> arrayList = new ArrayList<String>();
int i = 0;
public void readdata() throws Exception {
RandomAccessFile instream = new RandomAccessFile("in.txt", "r");
String str;
while (true) {
str = instream.readLine();
if (str == null)
break;
else {
arrayList.add(str);
i++;
}
}
instream.close();
}
public void Sortbyfirst() {
Collections.sort(arrayList, new sort());
}
public void outputdata() throws Exception {
File file = new File("out.txt");
file.delete();
RandomAccessFile outstream = new RandomAccessFile("out.txt", "rw");
for (int k = 0; k < i; k++) {
outstream.writeBytes(arrayList.get(k) + "\n");
}
}
}
class sort implements Comparator {
@Override
public int compare(Object o1, Object o2) {
String str1 = (String) o1;
String str2 = (String) o2;
return str1.compareTo(str2);
}
}