このループを使用して、出力が異なるさまざまなテキストファイルを作成する必要があります。現在、次のような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();
}