我想知道如何让程序在每次运行时输出一个新的文本文件。例如首先运行machineslot(1).txt,第二次运行machineslot(2).txt,等等。或者,在制作文件时使输出文件包含。每次我的程序运行时,如何输出新的文本文件?
File file = new File("MachineSlot.Txt");
try(PrintWriter out = new PrintWriter(new FileWriter(file));) {
for (int i = 0; i < winData.length; i++){
if (winData[i][0] != 0.0) {
out.printf("You won Machine %.0f. You won $%.2f. You have %.0f quarters which equals $%.2f %n", winData[i][0], winData[i][1], winData[i][2], winData[i][3]);
}
}
for (int k = 0; k < plays.length; k++)
out.println("You were able to play machine " + (k + 1) +" a total of "+ plays[k] + " times.");
}//end of try.
catch(IOException error){
System.out.println("Could not use the IO file");
}//End catch
为什么你需要PrintWriter? – Lokesh
如果循环再次运行,你还想写一个不同的文件吗?或附加在现有的文件? – Lokesh