csv 파일을 List<String>으로

in_ho_·2022년 11월 21일
0

CommonFunc

목록 보기
1/2
post-thumbnail
    /**
     * CSV를 읽을 수 있는 메서드
     * @param csvPath - csv 파일 경로
     * @return csvList - csv 라인을 갖고 온 List
     */
    public List<String> readCSV(String csvPath) {
        List<String> csvList = new ArrayList<>();
    
        BufferedReader br = null;
        FileInputStream fis = null;
        InputStreamReader isr = null;
            
        String line = "";

        try {
            fis = new FileInputStream(csvPath);
            isr = new InputStreamReader(fis, "EUC-KR"); // 한글 인코딩
            br = new BufferedReader(isr);
            
            while ((line = br.readLine()) != null) { // 파일 담기
            	/* 데이터 가공 영역 */
            	csvList.add(line);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(br != null) br.close();
                if(fis != null) fis.close();
                if(isr != null) isr.close();
            } 
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        return csvList;
    }
  • 해당 메서드를 사용하면 코드를 List 형태로 갖고 올 수 있습니다.

0개의 댓글