LoginSignup
0
2

More than 5 years have passed since last update.

Spring Batchで区切り文字を変える with DelimitedLineTokenizer

Posted at

Spring Batchで区切り文字を変える with DelimitedLineTokenizer

Spring BatchのGetting Startedのサンプルを試して、読み込むダンプファイルの区切り文字を変更したくて調べました。ググって一発でヒットしなかったので、ブログネタではありますがQiitaにも投稿しておきます。

DelimitedLineTokenizerのコンストラクタに引数として区切り文字を渡す

これだけです。
DelimitedLineTokenizer APIにあるように、デフォルトコンストラクタは、カンマ区切りとなります。
カンマ以外で区切り場合は、コンストラクタに区切り文字を渡します。

public DelimitedLineTokenizer()
Create a new instance of the DelimitedLineTokenizer class for the common case where the delimiter is a comma.

public DelimitedLineTokenizer(java.lang.String delimiter)
Create a new instance of the DelimitedLineTokenizer class.

サンプル

reader.setLineMapper(new DefaultLineMapper() {
  {
    this.setLineTokenizer(new DelimitedLineTokenizer("\t") {
      {
        setNames(new String[] {"firstName", "lastName"});
      }
    });
    setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
      {
        setTargetType(Person.class);
      }
    });
  }
});

これでタブ区切りのファイルもちゃんと読み込めました。その他の部分は上記公式サンプルを参照願います。

0
2
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
2