0

我正在尝试生成一行 10000 个随机数并将其存储在一个文件中。我继续在以空格分隔的行中添加新的随机数。但是经过几次迭代后,附加的字符串变为空(?),当我尝试使用 System.out.println 在控制台上打印该行时它没有显示,并且没有任何内容写入文件中。对于 n=10、n=100、n=1000,该代码工作得非常好。

在我的代码下面找到

import java.io.BufferedWriter;
import java.io.FileWriter;

public class RandomNumber {

    public static void main(String[] args) {

    try {

        int n = 10000;
        String line = ""; // Have tried StringBuilder too, doesn't help
        for (int i = 0; i < n; i++) {
            int temp = (int) Math.ceil((Math.random() * n));
            line = line+temp+" ";
            System.out.println(line);
        }
        FileWriter fstream = new FileWriter("input" + n + ".txt");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(line);
        out.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    }
}

编辑:我已经删除了用于 out.write 的 for 循环,这不是必需的。我在使用 String 数组时使用过它。

4

4 回答 4

0

您正在创建 1000 个文件,而不是创建具有 1000 个随机数的单个文件,请检查此。请尝试运行它,它会有所帮助。

try {

    int n = 10;
    String line = "";
    for (int i = 0; i < n; i++) {
        int temp = (int) Math.ceil((Math.random() * n));
        line = line+temp+" ";
        System.out.println(line);
    }
    FileWriter fstream = new FileWriter("D:/workspace/StackOverflow/src/fileread/test.txt");
    BufferedWriter out = new BufferedWriter(fstream);
              out.write(line);

    out.close();
} catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}

控制台输出:

4 
4 10 
4 10 7 
4 10 7 1 
4 10 7 1 4 
4 10 7 1 4 5 
4 10 7 1 4 5 1 
4 10 7 1 4 5 1 4 
4 10 7 1 4 5 1 4 8 
4 10 7 1 4 5 1 4 8 4 

test.txt——内容

4 10 7 1 4 5 1 4 8 4 
于 2012-12-01T07:55:48.190 回答
0

切勿在循环内使用连接。StringBuilder以这种方式使用:

import java.io.BufferedWriter;
import java.io.FileWriter;

public class RandomNumber {

    public static void main(String[] args) {

    try {

        int n = 10000;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int temp = (int) Math.ceil((Math.random() * n));
            sb.append(temp).append(" ");
        }
        FileWriter fstream = new FileWriter("input" + n + ".txt");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(sb.toString());
        out.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    }
}
于 2012-12-01T08:08:45.693 回答
0

试试这个

import java.io.BufferedWriter;
import java.io.FileWriter;

public class RandomNumber {

    public static void main(String[] args) {

    try {

        int n = 10000;
        String line = "";
        for (int i = 0; i < n; i++) {
            int temp = (int) Math.ceil((Math.random() * n));
            line = line+temp+" ";
            System.out.println(line);
        }

        System.out.println("file name is :: input" + n + ".txt");
        FileWriter fstream = new FileWriter("input" + n + ".txt");
        BufferedWriter out = new BufferedWriter(fstream);
        // till here you were correct.
        // you don't need the loop here...
        out.write(line);
        out.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    }
}

也看看这里


编辑 1

由于您的程序在 n=10、n=100、n=1000 时运行良好,我建议您在 Eclipse 中增加堆大小。

于 2012-12-01T08:19:59.830 回答
-1

如果你只想要一个非常长的行,你应该在循环中编写,而不是将它存储在变量中。

我认为问题在于字符串太长(我得到了一个 48kb 的文件,下面的代码)以至于它内存不足。虽然奇怪的是它不会抛出 OutOfMemoryException 或类似的。

public static void main(String[] args) {

    try {

        int n = 10000;
        FileWriter fstream = new FileWriter("input" + n + ".txt");
        BufferedWriter out = new BufferedWriter(fstream);

        for (int i = 0; i < n; i++) {
            int temp = (int) Math.ceil((Math.random() * n));
            out.write(temp+" ");
            out.flush();
        }
        out.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}
于 2012-12-01T08:02:00.680 回答