Bibi's DevLog ๐Ÿค“๐ŸŽ

[Java] CSV ํŒŒ์ผ ์ž…์ถœ๋ ฅ (์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์ด์–ด์“ฐ๊ธฐ) ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Java ์ž๋ฐ”

[Java] CSV ํŒŒ์ผ ์ž…์ถœ๋ ฅ (์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์ด์–ด์“ฐ๊ธฐ)

๋น„๋น„ bibi 2021. 2. 4. 22:57

Java CSV ํŒŒ์ผ ์ž…์ถœ๋ ฅ (์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์ด์–ด์“ฐ๊ธฐ)

๋งํฌ1์—์„œ ์ฝ๊ธฐ๋ฅผ, ๋งํฌ2์—์„œ ์“ฐ๊ธฐ๋ฅผ, ๋งํฌ3 ์—์„œ ์ด์–ด์“ฐ๊ธฐ๋ฅผ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.

CSV๋ž€?

: Comma-Separated Values์˜ ์•ฝ์ž๋กœ, ๊ฐ ํ•„๋“œ๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ํ…์ŠคํŠธ ํŒŒ์ผ์ด๋‹ค.
๋ณดํ†ต ๋งจ ์ฒซ ์ค„์—๋Š” ์ปฌ๋Ÿผ ์ด๋ฆ„์ด ๋ถ™์–ด ์žˆ๋‹ค.

name,height,weight
AA,180,80
BB,170,65

.csvํŒŒ์ผ์€ ๊ธฐ๋ณธ ๋ฉ”๋ชจ์žฅ์—์„œ๋„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ €์žฅ์‹œ ํ™•์žฅ์ž๋ฅผ .csv๋กœ ์ง€์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
(ํŒŒ์ผ์— ํ•œ๊ธ€์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ, ์ €์žฅ์‹œ ์ธ์ฝ”๋”ฉ์„ UTF-8๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค)

Java CSVํŒŒ์ผ ์ฝ๊ธฐ

import java.io.*;
import java.util.*;

public class CSVReader {
    public static void main(String[] args) {
        CSVReader csvReader = new CSVReader();
        csvReader.readCSV();
    }

    public List<List<String>> readCSV() {
        List<List<String>> csvList = new ArrayList<List<String>>();
        File csv = new File("์—ฌ๊ธฐ์— .csvํŒŒ์ผ์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค");
        BufferedReader br = null;
        String line = "";

        try {
            br = new BufferedReader(new FileReader(csv));
            while ((line = br.readLine()) != null) { // readLine()์€ ํŒŒ์ผ์—์„œ ๊ฐœํ–‰๋œ ํ•œ ์ค„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜จ๋‹ค.
                List<String> aLine = new ArrayList<String>();
                String[] lineArr = line.split(","); // ํŒŒ์ผ์˜ ํ•œ ์ค„์„ ,๋กœ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด์— ์ €์žฅ ํ›„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
                aLine = Arrays.asList(lineArr);
                csvList.add(aLine);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) { 
                    br.close(); // ์‚ฌ์šฉ ํ›„ BufferedReader๋ฅผ ๋‹ซ์•„์ค€๋‹ค.
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        return csvList;
    }
}

Java CSVํŒŒ์ผ ์“ฐ๊ธฐ/์ด์–ด์“ฐ๊ธฐ

public void writeCSV() {
        File csv = new File("์—ฌ๊ธฐ์— .csvํŒŒ์ผ์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค");
        BufferedWriter bw = null; // ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ
        try {
            bw = new BufferedWriter(new FileWriter(csv, true));
            // csvํŒŒ์ผ์˜ ๊ธฐ์กด ๊ฐ’์— ์ด์–ด์“ฐ๋ ค๋ฉด ์œ„์ฒ˜๋Ÿผ true๋ฅผ ์ง€์ •ํ•˜๊ณ , ๊ธฐ์กด ๊ฐ’์„ ๋ฎ์–ด์“ฐ๋ ค๋ฉด true๋ฅผ ์‚ญ์ œํ•œ๋‹ค

            for (int i = 0; i < dataList.size(); i++) {
                String[] data = dataList.get(i);
                String aData = "";
                aData = data[0] + "," + data[1] + "," + data[2] + "," + data[3];
                // ํ•œ ์ค„์— ๋„ฃ์„ ๊ฐ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์— ,๋ฅผ ๋„ฃ๋Š”๋‹ค
                bw.write(aData);
                // ์ž‘์„ฑํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์— ๋„ฃ๋Š”๋‹ค
                bw.newLine(); // ๊ฐœํ–‰
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.flush(); // ๋‚จ์•„์žˆ๋Š” ๋ฐ์ดํ„ฐ๊นŒ์ง€ ๋ณด๋‚ด ์ค€๋‹ค
                    bw.close(); // ์‚ฌ์šฉํ•œ BufferedWriter๋ฅผ ๋‹ซ์•„ ์ค€๋‹ค
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }