我正在尝试生成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数组时使用过它。
答案 0 :(得分: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-- content
4 10 7 1 4 5 1 4 8 4
答案 1 :(得分: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());
}
}
}
答案 2 :(得分: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());
}
}
}
另请查看here。
由于你的程序工作正常,n = 10,n = 100,n = 1000,我建议你增加Eclipse中的堆大小。
答案 3 :(得分:-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());
}
}