Java 我的PrintWriter正在覆盖我打印的每一行。如何让它记录我程序中的所有内容?
当我尝试用PrintWriter记录所有失败的尝试时,它只会覆盖日志中的每一行,而不是在每次尝试失败时在日志中生成多行。我该如何补救?怎么回事 包装HW1Java 我的PrintWriter正在覆盖我打印的每一行。如何让它记录我程序中的所有内容?,java,Java,当我尝试用PrintWriter记录所有失败的尝试时,它只会覆盖日志中的每一行,而不是在每次尝试失败时在日志中生成多行。我该如何补救?怎么回事 包装HW1 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class Banking { public static void main(String
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Banking {
public static void main(String[] args) throws FileNotFoundException {
File fi1 = new File("ClientInfo.txt");
Scanner sc1 = new Scanner(fi1);
Scanner sc2 = new Scanner(System.in);
Scanner sc3 = new Scanner(System.in);
Scanner loginScan = new Scanner(System.in);
ClientRecords[] clientRecordArray = new ClientRecords[100];
PrintWriter logTransaction1 = new PrintWriter("BankLogs.txt");
String firstName;
String middleName;
String lastName;
String userId = "";
String userIdTyped;
int pinNumber;
double balance;
String choice1;
int choice2;
int logSize = 0;
int failedUserId = 0;
while(sc1.hasNext())
{
firstName = sc1.next();
middleName = sc1.next();
lastName = sc1.next();
userId = sc1.next();
pinNumber = sc1.nextInt();
balance = sc1.nextDouble();
ClientRecords client1 = new ClientRecords(firstName, middleName, lastName, userId, pinNumber, balance);
clientRecordArray[logSize] = client1;
logSize++;
}
System.out.println("Please enter your user ID:");
userIdTyped = loginScan.nextLine();
while(!clientRecordArray[0].getUserId().equals(userIdTyped))
{
userIdTyped = null;
System.out.println("Wrong user ID please try again:");
failedUserId++;
userIdTyped = loginScan.nextLine();
}
logTransaction1.println("Failed to enter user ID " + failedUserId + " times.");
logTransaction1.close();
menu1(sc2);
menu2(sc3);
}
public static void menu1(Scanner sc2)
{
ClientRecords client1 = new ClientRecords("N", "xx", "uygg", "PinkFloyd111", 0007, 51234.99);
System.out.println("Please enter the corresponding pin number:");
int userPin = sc2.nextInt();
while(userPin != client1.getUserPin())
{
System.out.println("Sorry wrong PIN please try again.");
userPin = sc2.nextInt();
}
}
public static void menu2(Scanner sc3) throws FileNotFoundException
{
PrintWriter logTransaction = new PrintWriter("BankLogs.txt");
ClientRecords client1 = new ClientRecords("hjhh", "hjh", "hh", "PinkFloyd111", 0007, 51234.99);
int choice = 0;
double depositAmount = 0;
double withdrawAmount = 0;
double balance = client1.getUserBalance();
System.out.println("Welcome to your bank!");
System.out.println("Press (1) to deposit to your account. ");
System.out.println("Press (2) to withdraw from your account. ");
System.out.println("Press (3) to check balance of your account. ");
System.out.println("Press (4) to exit the system.");
System.out.println("Enter the number for your choice here: ");
choice = sc3.nextInt();
switch (choice) {
case 1:
System.out.println("Please enter how much you'd like to desposit here:");
depositAmount = sc3.nextDouble();
balance += depositAmount;
System.out.println("You have deposited " + "$" + depositAmount);
logTransaction.println("Deposit of: " + "$" + depositAmount);
logTransaction.close();
break;
case 2:
System.out.println("Please enter how much you'd like to withdraw here:");
withdrawAmount = sc3.nextDouble();
balance -= withdrawAmount;
System.out.println("You have withdrawn " + "$" + withdrawAmount);
logTransaction.println("Withdrawal of: " + "$" + withdrawAmount);
logTransaction.close();
break;
case 3:
System.out.println("The balance of your account is: " + "$" + balance);
break;
case 4:
System.out.println("Thank you and have a great day!");
}
}
}
每次你打电话
PrintWriter logTransaction = new PrintWriter("BankLogs.txt");
您正在截断文件并重新启动。要追加,需要显式调用FileWriter
PrintWriter logTransaction = new PrintWriter(new FileWriter("BankLogs.txt", true), true);
这允许附加到现有文件。另外,第二个true启用自动刷新,因此如果程序崩溃,您丢失数据的可能性更小。Post a。没别的办法,是的。我是新来的,想边走边学习网站。