자바 csv 유틸

조회 1,209 · 댓글 0
¯¯\_(ツ)_/¯작성자2019년 9월 19일
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);
}
}
}

로그인 후 답글을 남길 수 있습니다.