Skip to content

Instantly share code, notes, and snippets.

@donchan922
Last active July 7, 2019 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donchan922/e724e79b6d2bfaf3aefeebd9c286211b to your computer and use it in GitHub Desktop.
Save donchan922/e724e79b6d2bfaf3aefeebd9c286211b to your computer and use it in GitHub Desktop.
public class DemoApplication {
public static void main(String[] args) throws IOException {
CsvMapper mapper = new CsvMapper();
// ヘッダなし
CsvSchema schema = mapper.schemaFor(User.class);
Path path = Paths.get("CSVファイルのパス");
try (BufferedReader br = Files.newBufferedReader(path)) {
MappingIterator<User> it = mapper.readerFor(User.class).with(schema).readValues(br);
// CSVファイルを1行ずつ読み込む場合
while (it.hasNextValue()) {
User user = it.nextValue();
// User(id=001, name=Alice, age=18)
// User(id=002, name=Bob, age=25)
// User(id=003, name=Carol, age=23)
System.out.println(user.toString());
}
// CSVファイルを全行まとめて読み込む場合
List<User> userList = it.readAll();
for (User user : userList) {
// User(id=001, name=Alice, age=18)
// User(id=002, name=Bob, age=25)
// User(id=003, name=Carol, age=23)
System.out.println(user.toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment