LoginSignup
3
3

More than 5 years have passed since last update.

【Java】巨大なテキストファイルを生成するメソッド

Last updated at Posted at 2016-06-06

パフォーマンスを測るとき、巨大なテキストファイルが欲しいときがある。そんなわけで、1行100文字の100万行からなるテキストファイル(約100MB)を生成するメソッドを作った。

// インポート宣言
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import org.apache.commons.lang3.RandomStringUtils;

// メソッド定義
public static void createHugeFile(final String path) throws IOException {
    Iterable<String> lines = () -> Stream
        .generate(() -> RandomStringUtils.randomAlphanumeric(100))
        .limit(1000000).iterator();
    Files.write(Paths.get(path), lines);
}

RandomStringUtils.randomAlphanumeric()を使用するには、org.apache.commons.lang3_X.X.X.vXXXXXXXXXXXX.jarをビルドパスに追加する必要がある。このJARファイルは、Eclipseのpluginsフォルダにあった。

3
3
0

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