자바 csv 유틸

  • rmrf
    (¯\_(ツ)_/¯)
  • rmrf's Avatar 이 글의 작성자
  • Offline
  • Newbie
  • Newbie
더보기
19 Sep 2019 17:02 #4859 작성자: rmrf
rmrf 님의 글: 자바 csv 유틸
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CsvUtils {

    public static String readFile(String path) {
        try {
            Scanner scanner = new Scanner(new File(path)).useDelimiter("\\A");

            if (scanner.hasNext()) {
                return scanner.next();
            } else {
                return "";
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> readFromCsv(ObjectReader objectReader, String path) {
        try {
            List<T> results = new ArrayList<>();

            MappingIterator<T> iterator = objectReader.readValues(readFile(path));

            while (iterator.hasNext()) {
                results.add(iterator.nextValue());
            }

            return results;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Time to create page: 0.057 seconds
Powered by Kunena Forum