数据写入文件
【实验程序原码】
package shiyan5;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class bank {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账户号码:");
String accountNumber = scanner.nextLine();
System.out.println("请输入账户名称:");
String accountName = scanner.nextLine();
System.out.println("请输入账户余额:");
double balance = scanner.nextDouble();
BankAccount account = new BankAccount(accountNumber, accountName);
account.deposit(balance);
System.out.println("存入成功!当前余额为:" + account.getBalance());
System.out.print("是否要写入文件?(y/n)");
String choice = scanner.next() ;
if (choice.equalsIgnoreCase("y")) {
account.writeToFile("account.txt");
System.out.println("写入成功!");
} else {
System.out.println("未写入文件。");
}
scanner.close();
ArrayList<BankAccount> account1= BankAccount.readFromFile("account.txt");
for (BankAccount bankAccount:account1
) {
System.out.println(bankAccount);
}
}
interface AccountInterface {
void deposit(double amount);
void withdraw(double amount);
}
static class BankAccount implements AccountInterface {
private double balance = 0.0;
private String accountNumber;
private String accountName;
public BankAccount(String accountNumber, String accountName) {
this.accountNumber = accountNumber;
this.accountName = accountName;
}
public void deposit(double amount) {
balance += amount;
System.out.println("存款成功!当前余额为:" + balance);
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountName() {
return accountName;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String toString(){
return "---账号名:"+this.accountName+",账号:"+this.accountNumber+",余额:"+this.balance;
}
public void withdraw(double amount) {
if (amount > balance) {
System.out.println("余额不足,取款失败!");
} else {
balance -= amount;
System.out.println("取款成功!当前余额为:" + balance);
}
}
public void writeToFile(String fileName) {
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName,true))) {
writer.println(accountNumber + "," + accountName + "," + balance);
} catch (IOException e) {
System.out.println("写入文件失败:" + e.getMessage());
}
}
public static ArrayList<BankAccount> readFromFile(String fileName) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
String[] parts;
double balance = 0.0;
String accountNumber = "";
String accountName = "";
ArrayList<BankAccount> bankAccounts=new ArrayList<BankAccount>();
while ((line = reader.readLine()) != null) {
parts = line.split(",");
accountNumber = parts[0];
accountName = parts[1];
balance = Double.parseDouble(parts[2]);
BankAccount bankAccount=new BankAccount(accountNumber, accountName);
bankAccount.setBalance(balance);
bankAccounts.add(bankAccount);
}
return bankAccounts;
} catch (IOException e) {
System.out.println("读取文件失败:" + e.getMessage());
}
return null; // 如果文件格式不正确或读取错误,返回null。但这种错误应该在开发过程中尽量避免。
}
}
}
【实验结果】