Last active
July 7, 2019 06:57
-
-
Save donchan922/e724e79b6d2bfaf3aefeebd9c286211b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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