-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGi_chul_2017.java
187 lines (150 loc) · 4.61 KB
/
Gi_chul_2017.java
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
package mocktest1;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Gi_chul_2017 {
public static void main(String[] args) throws IOException {
ArrayList<String> al = new ArrayList<String>();
al=readfile("data2.txt");
//writeTypeCount(al);
//writeManyTypeCount(al);
writeManyTypeCountAndLog(al);
writeManyTypeThreadCount(al);
}
private static void writeManyTypeThreadCount(ArrayList<String> al) {
ExecutorService executor = Executors.newFixedThreadPool(3);
for(int i=0; i<al.size(); i++) {
if(al.get(i).equals("")) continue;
Runnable worker = new WorkerThread(al.get(i));
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
private static void writeManyTypeCountAndLog(ArrayList<String> al) throws IOException {
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
for(int i=0; i<al.size(); i++) {
if(al.get(i).equals("")) continue;
String[] spl = al.get(i).split("#");
String type=spl[1];
if(hm.containsKey(type)) {
ArrayList <String> newal=hm.get(type);
String temp=spl[0]+"#"+spl[1]+"#"+spl[2]+"#"+"AAA";
newal.add(temp);
hm.put(type, newal);
}else if(!hm.containsKey(type)) {
ArrayList <String> newal=new ArrayList<String>();
String temp=spl[0]+"#"+spl[1]+"#"+spl[2]+"#"+"AAA";
newal.add(temp);
hm.put(type, newal);
}
}
for(String key: hm.keySet()) {
String typeFileName="type_3_"+key+".txt";
File file = new File("./TEXT/", typeFileName);
PrintWriter pw = new PrintWriter(new FileWriter(file, true));
for(String temp:hm.get(key)) {
pw.println(temp);
}
pw.close();
}
}
private static void writeManyTypeCount(ArrayList<String> al) throws FileNotFoundException {
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i=0; i<al.size(); i++) {
if(al.get(i).equals("")) continue;
String[] spl = al.get(i).split("#");
String type=spl[1];
if(hm.containsKey(type)) {
int temp= hm.get(type);
temp++;
hm.put(type,temp);
}else if(!hm.containsKey(type)) {
hm.put(type, 1);
}
}
String outputFileName="REPORT_2.txt";
File file = new File("./TEXT/", outputFileName);
PrintWriter pw = new PrintWriter(file);
for(String key: hm.keySet()) {
pw.println(key+"#"+hm.get(key));
}
pw.close();
}
private static void writeTypeCount(ArrayList<String> al) {
int ee=0;
int ww=0;
int wx=0;
for(int i=0; i<al.size(); i++) {
if(al.get(i).equals("")) continue;
String[] spl = al.get(i).split("#");
if(spl[1].equals("EE")) {
ee++;
}else if(spl[1].equals("WW")) {
ww++;
}else if(spl[1].equals("WX")) {
wx++;
}
}
String outputFileName="REPORT_1.txt";
File file = new File("./TEXT/", outputFileName);
try {
PrintWriter pw = new PrintWriter(file);
pw.println("EE#"+ee);
pw.println("WW#"+ww);
pw.println("WX#"+wx);
pw.close();
}catch(Exception e) {}
}
private static ArrayList<String> readfile(String fileName) {
String line = null;
ArrayList<String> al = new ArrayList<String>();
try {
File file = new File("./TEXT/", fileName);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
al.add(line);
}
bufferedReader.close();
}catch(Exception e) {e.printStackTrace();}
return al;
}
}
class WorkerThread implements Runnable {
private String command;
public WorkerThread(String s){
this.command=s;
}
@Override
public void run() {
//System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
System.out.println(Thread.currentThread().getName());
try {
processCommand(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(Thread.currentThread().getName()+" End.");
}
private void processCommand(String str) throws IOException {
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
String[] spl = str.split("#");
String type = spl[1];
String filename = "type_4_"+type+".txt";
File file = new File("./TEXT/", filename);
PrintWriter pw = new PrintWriter(new FileWriter(file, true));
pw.println(str);
pw.close();
}
}