リソース ファイル (たとえば ) のランタイム クラスパスをスキャンし、そのハンドルをres/config/meta.cfg
作成する必要がある状況があります。File
私が思いついた最高のものは次のとおりです。
// This file is located inside a JAR that is on the runtime classpath.
String fileName = "res/config/meta.cfg";
try {
InputStream inStream = ClassLoader.getSystemResourceAsStream(fileName);
File file = new File(String.format("${java.io.tmpdir}/%s", fileName));
FileOutputStream foutStream = null;
foutStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while((read = inStream.read(bytes)) != -1)
foutStream.write(bytes, 0, read);
foutStream.close();
return file;
} catch (Exception exc) {
throw new RuntimeException(exc);
}
基本的に、リソースを として読み込みInputStream
、ストリームを一時ファイル ( の下) に書き込んで、有効なハンドルを{$java.io.tmpdir}
取得できるようにします。File
これは、納屋の周りを 3 方向に移動するようです。これを行うためのより良い/より簡単な/よりエレガントな方法はありますか? 前もって感謝します!