클로이의 데이터 여행기

[JAVA] 1:다(多) 데이터를 1:1 데이터로 바꾸기(전환) 본문

JAVA

[JAVA] 1:다(多) 데이터를 1:1 데이터로 바꾸기(전환)

징느K 2019. 4. 26. 16:09

이번 포스팅에서 다룰 내용은 1:다 데이터를 1:1로 바꾸는 방법입니다.

아래의 포스팅 내용과 반대되는 내용입니다. 동일한 값(KEY값,대표어)를 기준으로 결합하는 방법이 궁금하시다면 링크의 포스팅을 참고하시기 바랍니다.  (https://data-traveler.tistory.com/19?category=719589)

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
package synForR;
 
 
public class GetSynList {
    static String NEWLINE = System.getProperty("line.separator");
    public static ArrayList<String> getDitionary() {
        File file = new File("C:/data/input/dev_index_syn_user.txt");
        
        String line = "";
        ArrayList<String> dicList = new ArrayList<String>();
 
        try {
            BufferedReader inFiles = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file.getAbsolutePath()), "UTF8"));
            while ((line = inFiles.readLine()) != null) {
                dicList.add(line);
            }
            inFiles.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dicList;
    }
 
    public static ArrayList<String> makeDicForR(ArrayList<String> dicList) {
        ArrayList<String> SynDic = new ArrayList<String>();
 
        for (int j = 0; j < dicList.size(); j++) {
            String[] synLine = dicList.get(j).split(",");
            
            for (int k = 0; k < synLine.length -1; k++) {
                SynDic.add(synLine[0]+",");
                SynDic.add(synLine[k+1]+"\n");
            }
        }
        return SynDic;
    }
    
    public static void writeFile(ArrayList<String> SynDic) {
        String filename = "C:/data/output/getSynList.txt";
        try {
            File output = new File(filename);
            BufferedWriter writer = new BufferedWriter(new FileWriter(output));
 
            for (int k = 0; k<SynDic.size(); k++) {
                writer.write(SynDic.get(k));
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        ArrayList<String> origin = getDitionary();
        ArrayList<String> SynDic = makeDicForR(origin);
        writeFile(SynDic);
        System.out.println("==============END==============");
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

이상입니다.

읽어주셔서 감사합니다 : ) 

Comments