dT*blog

design and programming

クラスパス外のリソースを読み込む

「リソースファイルを WEB-INF/classes 以外の場所に配置できないもんか」の続き。

クラスローダーから見えない properties ファイルは参照できないんだなー、残念だなー、今日の雨は秋の訪れを予感させるなー、おでん食べたいなー、とか思っていたのだけど、実は相対パスで上の階層まで見れることが判明!

ClassLoader loader;
InputStream inputStream = null;

String path = "../../hoge/test.properties"; Properties properties = new Properties();
try { loader = Thread.currentThread().getContextClassLoader(); inputStream = new BufferedInputStream(loader.getResourceAsStream(path)); properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { // } } }

こんなことができるとは知らなんだ。頭は柔軟じゃないとダメっすね。

Posted by dT by 11:25

トラックバック

このエントリーのトラックバックURL
http://www.deftrash.com/admin/mt/mt-tb.cgi/351

コメント




保存しますか?

(書式を変更するような一部のHTMLタグを使うことができます)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30