0
0

More than 3 years have passed since last update.

springでクラスパス下のファイルを文字列として読み込む

Posted at

とりあえずsrc/main/resources/application.ymlを文字列として読み込むコード。

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import jp.co.rakuten.firstparty.base.exception.FirstPartyBizException;

public class Hoge {

  public static void main(String[] args) {

    String path = "application.yml";
    try {
      String str = new String(FileCopyUtils.copyToByteArray(new ClassPathResource(path).getInputStream()),
          StandardCharsets.UTF_8);
      System.out.println(str);
    } catch (IOException e) {
      throw new FirstPartyBizException(e);
    }
  }

}

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