0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SpringBootで、CSVを読み込んでList<String>に変換する

Last updated at Posted at 2023-10-17

環境

名称 バージョン
Spring Boot 3.1.4
opencsv 5.8

問題

SpringBoot環境下でアップロードされたCSVファイル@MultipartFileを、サーバーにファイルを置かずにリストに変換するのに右往左往してしまったので覚え書き。

~条件~
・CSVの文字コードはUTF-16
・区切り文字はタブ文字
・改行文字が入っててもサクッと解決

解決方法

sampleService.java
  public List<String[]> csvToList(MultipartFile file) {
    // CSVをListに変換
    Reader reader = new InputStreamReader(file.getInputStream(), "UTF_16");
    CSVParser parser = new CSVParserBuilder().withSeparator('\t').build();
    CSVReader csvReader = new CSVReaderBuilder(reader).withCSVParser(parser).build();
    List<String[]> taskList = csvReader.readAll();
  }

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?