대용량 데이터 처리하기
파일 읽어 오는 기능
전입코드, 전출코드를 파싱하는 기능
시도 한글로 매핑
중복 개수 세는 기능
새로운 파일 만드는 기능
파일에 내용 적는 기능
barChart
heatMap
PopulationMove
package day4;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class PopulationMove {
private int fromSido; // 전입
private int toSido; // 전출
public PopulationMove() {
}
public PopulationMove(String fromSido, String toSido) { // 생성자를 통한 값 할당
this.fromSido = Integer.parseInt(fromSido);
this.toSido = Integer.parseInt(toSido);
}
public int getFromSido() { //getter
return fromSido;
}
public int getToSido() { //getter
return toSido;
}
}
Java
복사
PopulationStatistics
package day4;
import java.awt.datatransfer.StringSelection;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class PopulationStatistics {
// 파일 라인 단위로 읽기 (모래를 삽으로 푸는것과 같음)
public List<PopulationMove> readByLineV1(String filename) throws IOException{
List<PopulationMove> pml = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(filename)); //삽
String str;
while ((str = reader.readLine()) != null) {
PopulationMove pm = parse(str);
pml.add(pm);
}
reader.close();
return pml;
}
// 데이터를 , 기준으로 파싱하여 PopulationMove객체로 반환하는 메서드
public PopulationMove parse(String data) throws IOException {
String[] split = data.split(",");
return new PopulationMove(split[0],split[1]);
}
// filename 으로 파일을 만드는 메서드
public void createAFile(String filename) throws IOException {
File file = new File(filename);
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// String 리스트와 파일 이름을 인수로 받아 파일에 리스트 내용을 작성하는 함수
public void write(List<String> strs, String filename) {
File file = new File(filename);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (String str : strs) {
writer.write(str);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// pml리스트에서 갯수를 세어 맵에 넣어 반환
public Map<String, Integer> getMoveCntMap(List<PopulationMove> pml) {
HashMap<String, Integer> moveCntMap = new HashMap<>();
for (PopulationMove pm : pml) {
String key = pm.getFromSido() + "," + pm.getToSido();
if (moveCntMap.get(key) == null) { //최초 1로 초기화 작업
moveCntMap.put(key, 1);
}
moveCntMap.put(key, moveCntMap.get(key) + 1); // key가 중복될 시 1씩 더하여 삽입
}
return moveCntMap;
}
public static void main(String[] args) throws IOException {
PopulationStatistics populationStatistics = new PopulationStatistics(); // PopulationStatistics 객체 생성
List<PopulationMove> pml = populationStatistics.readByLineV1("./from_to.txt");
// 해당 이름의 파일 한 줄 단위로 타입이 PopulationMove인 리스트에 넣음
Map<String, Integer> map = populationStatistics.getMoveCntMap(pml);
// 중복 코드 갯수 map으로
System.out.println(map);
String targetname = "each_sido_cnt.txt";
populationStatistics.createAFile(targetname); //파일 생성
List<String> cntResult = new ArrayList<>();
for (String key : map.keySet()) {
String s = String.format("key:%s value:%d\n", key, map.get(key));
cntResult.add(s);
}
populationStatistics.write(cntResult,targetname); //파일 작성
}
}
Java
복사