Question

I am trying to generate a line of 10000 random number and store it in a file. I go on adding new random numbers to the line separated by spaces. But after few iterations, the appended string becomes empty(?), it doesn't show up when I try to print the line on console using System.out.println, and nothing is written into the file. The code works absolutely fine for n=10, n=100, n=1000.

Find below my code

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());
    }
    }
}

edit: I have removed the for loop used for out.write, it was not necessary. I had used it when I had used a String array.

Was it helpful?

Solution

Try this

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());
    }
    }
}

Also take a look here.


Edit 1

As your program is working fine with n=10, n=100, n=1000, I would suggest you to increase the heap size in Eclipse.

OTHER TIPS

You are creating 1000 files instead of creating single file with 1000 random numbers check this. Please try to run this , it will be helpful.

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());
}

Console output :

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

4 10 7 1 4 5 1 4 8 4 

Never use concatenation inside loops. Use StringBuilder in this way:

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());
    }
    }
}

if you just want one really long line you should write in the loop, not storing it in a variable.

i think the problem is that the string just gets so long (i got a file with 48kb with code below) that it gets out of memory. though strange that it doesnt throw an OutOfMemoryException or similar.

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());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top