ページ

2011-04-20

apache commons ioは便利.2

こんな書き方もできる.





import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;

public class FileReadSample{

public FileReadSample(final File file) throws IOException{

InputStream in = null;
try{
in = new FileInputStream(file);
List lines = IOUtils.readLines(in);

for( String line : lines ){
System.out.println(line);
}
}finally{
IOUtils.closeQuietly(in);
}
}

public static void main(String[] args) throws IOException {
if( args.length == 0 ){
throw new IllegalArgumentException();
}
new FileReadSample(new File( args[0]));
}

}

0 件のコメント:

コメントを投稿