Java:用随机数创建大文本文件?

时间:2014-07-25 15:28:35

标签: java

我试图在每一行创建一个随机数字的文本文件。

我已设法做到这一点,但由于某种原因,我似乎生成的最大文件是768MBs,我需要高达15Gbs的文件。

为什么会发生这种情况?我的猜测是某种尺寸限制或内存问题?

这是我写的代码:

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        //Size in Gbs of my file that I want
        double wantedSize = Double.parseDouble("1.5");

        Random random = new Random();
        PrintWriter writer = new PrintWriter("AvgNumbers.txt", "UTF-8");
        boolean keepGoing = true;
        int counter = 0;
        while(keepGoing){
            counter++;
            StringBuilder stringValue = new StringBuilder();
            for (int i = 0; i < 100; i++) {
                double value = 0.1 + (100.0 - 0.1) * random.nextDouble();
                stringValue.append(value);
                stringValue.append(" ");
            }
            writer.println(stringValue.toString());
            //Check to see if the current size is what we want it to be
            if (counter == 10000) {
                File file = new File("AvgNumbers.txt");
                double currentSize = file.length();
                double gbs = (currentSize/1000000000.00);
                if(gbs > wantedSize){
                    keepGoing=false;
                    writer.close();
                }else{
                    writer.flush();
                    counter = 0;
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

这就是我编码的方式。它也会产生你想要的尺寸。

public static void main(String... ignored) throws FileNotFoundException, UnsupportedEncodingException {
    //Size in Gbs of my file that I want
    double wantedSize = Double.parseDouble(System.getProperty("size", "1.5"));

    Random random = new Random();
    File file = new File("AvgNumbers.txt");
    long start = System.currentTimeMillis();
    PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")), false);
    int counter = 0;
    while (true) {
        String sep = "";
        for (int i = 0; i < 100; i++) {
            int number = random.nextInt(1000) + 1;
            writer.print(sep);
            writer.print(number / 1e3);
            sep = " ";
        }
        writer.println();
        //Check to see if the current size is what we want it to be
        if (++counter == 20000) {
            System.out.printf("Size: %.3f GB%n", file.length() / 1e9);
            if (file.length() >= wantedSize * 1e9) {
                writer.close();
                break;
            } else {
                counter = 0;
            }
        }
    }
    long time = System.currentTimeMillis() - start;
    System.out.printf("Took %.1f seconds to create a file of %.3f GB", time / 1e3, file.length() / 1e9);
}

最后打印

Took 58.3 seconds to create a file of 1.508 GB

答案 1 :(得分:-2)

你永远不会清理你的StringBuilder,它会不断累积你存储的所有随机数字符串。在你写完之后做一个clear()。