Created
August 15, 2019 13:14
-
-
Save donchan922/480cb4660a1e008626f16f6f89d5ff43 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().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