我想知道如何讓程序在每次運行時輸出一個新的文本文件。例如首先運行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