import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test2 {
public static void main(String[] args) throws Exception {
//开启3个线程读取文件不同位置的数据
Thread t1=new Thread(new Runnable() {
public void run() {
try {
mapTask(1,100,"E:\\java增强\\day06\\day06\\words.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2=new Thread(new Runnable() {
public void run() {
try {
mapTask(101,200,"E:\\java增强\\day06\\day06\\words.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t3=new Thread(new Runnable() {
public void run() {
try {
mapTask(201,400,"E:\\java增强\\day06\\day06\\words.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
});
//开启线程
t1.start();
t2.start();
t3.start();
//谁调用join就先执行完,然后回到主线程(主线程main)
t1.join();
t2.join();
t3.join();
//开启汇总的reduce任务
Thread t4=new Thread(new Runnable() {
public void run() {
try {
reduceTask("E:\\\\java增强\\\\day06\\\\day06\\\\result.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t4.start();
}
//读取文件的方法
public static void mapTask(int StartNum,int EndNum,String path) throws Exception {
Map<String,Integer>map=new HashMap<>();
//读取文件(一行一行的读)
LineNumberReader lr = new LineNumberReader(new FileReader(new File(path)));
//lr.getLineNumber();
String line=null;
while((line=lr.readLine())!=null) {
//获取当前行号
int num=lr.getLineNumber();
if(num>=StartNum && num<=EndNum) {
String[] words = line.split("\\s");
for (String word : words) {
Integer n = map.getOrDefault(word, 0);
n++;
map.put(word, n);
}
}
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(new File("E:\\java增强\\day06\\day06\\result.txt"),true)));
Set<Entry<String, Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
//System.out.println(entry.toString());
bw.write(entry.toString());
bw.newLine();
}
bw.flush();//通过将所有已缓冲输出写入底层流来刷新此流。
bw.close();
lr.close();
}
//读取新文件,遍历,并排序
public static void reduceTask(String path) throws Exception {
Map<String,Integer>map=new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(path));
String line=null;
while((line=br.readLine())!=null) {
String[] words = line.split("=");
Integer count = map.getOrDefault(words[0], 0);
count+=Integer.parseInt(words[1]);
map.put(words[0], count);
}
Set<Entry<String, Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
System.out.println(entry);
}
br.close();
}
}