LoginSignup
7
6

More than 5 years have passed since last update.

Commons IOの便利だと感じたメソッド

Last updated at Posted at 2015-01-12

Commons IOの中で、私が便利だと感じたメソッドを紹介します。

FileUtils

copyInputStreamToFile

InputStreamをファイルへそのままコピーします。
ファイルアップロード処理で使用しました。

import org.directwebremoting.io.FileTransfer;

FileTransfer fileTransfer; //クライアントから渡されたファイル(input[type="file"]要素)
InputStream is = fileTransfer.getInputStream();
FileUtils.copyInputStreamToFile(is, new File("/tmp/file.txt");

2.5からはcopyToFileメソッドも利用できます。
違いは、コピー元のInputStreamがcloseされるか否か。
copyInputStreamToFileはcloseされますが、copyToFileはopenのままです。
ZipInputStreamを使うときに役立つそうですが、その理由は分かりませんでした。

ただ、2.5はまだダウンロードできないので、気にする必要はないでしょう。

7
6
1

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
7
6