Created
July 7, 2019 07:05
-
-
Save donchan922/dde9e24bc17131cb0831d70a5ef5181a 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).withHeader(); | |
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