我需要使用这个循环来创建具有不同输出的不同文本文件。现在它创建了 3 个文件,如下所示:
texts1.txt = some text
texts2.txt = texts1.txt + some text
texts3.txt = texts2.txt + some text
我的想法是FileWriter
通过命名对象来创建多个类对象,Fw[it]
以便我需要尽可能多的对象。不幸的是,在java中我不能这样做。有没有其他方法可以FileWriter
在循环中创建多个对象?
int count = 3;
for (int it = 0; it < count; it++) {
String xxx = "texts" + it + ".txt";
FileWriter Fw = new FileWriter(xxx);
Collections.shuffle(list);
Fw.write(met.prnt(list,temp));
Fw.close();
}
好的,它编译并运行,但它仍然有同样的问题:它创建了 3 个文件,如下所示:
texts1.txt = some text
texts2.txt = texts1.txt + some text
texts3.txt = texts2.txt + some text
但是,它应该是这样的:
texts1.txt = some text
texts2.txt = some text
texts3.txt = some text
现在代码如下所示:
int count = 3;
for (int it = 0; it < count; it++) {
Collections.shuffle(list);
String xxx = "texts" + it + ".txt";
FileWriter hah[] = new FileWriter[count];
hah[it] = new FileWriter(xxx,false);
hah[it].write(met.prnt(list,temp));
hah[it].flush();
hah[it].close();
}