Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class DemoApplication {
public static void main(String[] args) throws IOException {
CsvMapper mapper = new CsvMapper();
// ヘッダなし、タブ区切り
CsvSchema schema = mapper.schemaFor(User.class).withColumnSeparator('\t');
Path path = Paths.get("TSVファイルのパス");
try (BufferedReader br = Files.newBufferedReader(path)) {
MappingIterator<User> it = mapper.readerFor(User.class).with(schema).readValues(br);
// TSVファイルを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());
}
// TSVファイルを全行まとめて読み込む場合
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
You can’t perform that action at this time.