Apache commonsが便利な件(commons-vfs編)
http://d.hatena.ne.jp/daisuke-m/20080702/1214982943
ユーティリティ系ばっかりじゃ飽きるかな。ということで今回はcommons-vfs。
commons-vfs は、バーチャルファイルシステム。ファイルというのはあらゆる形で存在します。例えば、
これらのファイルを抽象化して、すべて同じ操作でファイルの読み込み(場合によっては書き込み)ができるようにしたのがこのライブラリ。
このエントリはあんまり重くしません。基本的だけど強力な使い方をひとつだけ。あとは全部応用するだけです。
public class Main { public static void main(final String[] args) throws Exception { FileObject resourceFile = VFS.getManager().resolveFile(args[0]); InputStream inputStream = resourceFile.getContent().getInputStream(); String content = IOUtils.toString(inputStream); System.out.println(content); }
第一引数に与えられたファイルを読んで表示。簡単ですね。たったこれだけで、色んな場所にあるファイルを読み出せるんです。第一引数に与える文字列の例はこんな感じ。
- file:///home/someuser/somedir
- file:///C:/Documents and Settings
- file://///somehost/someshare/afile.txt
- /home/someuser/somedir
- c:\program files\some dir
- c:/program files/some dir
- zip:http://somehost/downloads/somefile.zip
- http://somehost:8080/downloads/somefile.jar
- http://myusername@somehost/index.html
詳しくはこちら。 http://commons.apache.org/vfs/filesystems.html
というわけでね、軽くですが基本的な使い方だけご紹介しました。というか、俺もこれ以上のマニアックな使い方は分からないですw 便利な応用など、あったら※欄あたりで教えて下さい。
# java-jaにおいて、太一君の準備が出来たようなので、これにて。