Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Created August 13, 2013 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoyamkung/6217413 to your computer and use it in GitHub Desktop.
Save tomoyamkung/6217413 to your computer and use it in GitHub Desktop.
[Java]FileUtils#iterateFiles を使ったファイルフィルタのスニペット。
package net.tomoyamkung.commons.io.fileutils;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.File;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
public class FileUtilsTest {
@Test
public void 拡張子がtxtのFileオブジェクトを取得する() throws Exception {
// Setup
File directory = new File("testdata/commons_io_fileutils/fileutilstest");
String[] extensions = {"txt"};
boolean recursive = false;
// Exercise
Iterator<File> actual = FileUtils.iterateFiles(directory, extensions, recursive);
// Verify
assertThat(actual.hasNext(), is(true));
File next = actual.next();
assertThat(next.getName(), is("hoge1.txt"));
assertThat(actual.hasNext(), is(true));
next = actual.next();
assertThat(next.getName(), is("hoge2.txt"));
assertThat(actual.hasNext(), is(false));
}
}
@tomoyamkung
Copy link
Author

テストデータとして、"testdata/commons_io_fileutils/fileutilstest" ディレクトリを作成し、そこに次のファイルを用意する。

  • hoge1.txt
  • hoge2.txt
  • piyo.jpg
  • var.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment