Java IO流对象流实操
ATM的io对象流:
package com.jsu.atm;
import com.jsu.atm.Serializable;
public class Account implements Serializable{//私有数据成员private String UserName; // 用户名称private String PassWord; // 用户密码private double RemainMoney; // 用户余额private double withdrawal; // 用户单次取现额度private String CardId; // 用户账号public Account() {}public Account(String cardId, String userName, String password, double withdrawal) {CardId = cardId;UserName = userName;PassWord = password;this.withdrawal = withdrawal;}public String getUserName() {return UserName;}public void setUserName(String userName) {UserName = userName;}public String getPassWord() {return PassWord;}public void setPassWord(String passWord) {PassWord = passWord;}public double getRemainMoney() {return RemainMoney;}public void setRemainMoney(double remainMoney) {RemainMoney = remainMoney;}public double getWithdrawal() {return withdrawal;}public void setWithdrawal(double withdrawal) {this.withdrawal = withdrawal;}public String getCardId() {return CardId;}public void setCardId(String cardId) {CardId = cardId;}@Overridepublic String toString() {return "账户{" +"用户名='" + UserName + '\'' +", 密码='" + PassWord + '\'' +", 用户余额=" + RemainMoney +", 用户单次取现额度=" + withdrawal +", 卡号='" + CardId + '\'' +'}';}
}
package com.jsu.atm;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;public class IO {private IO() {}public static void addAccount(ArrayList<Account> accounts, FileOutputStream fos) throws IOException {// 将 ArrayList<Account> 转换为字符串String serializedData = serializeToString(accounts);// 将字符串转换为字节数组,并指定编码方式为UTF-8byte[] byteArray = serializedData.getBytes(StandardCharsets.UTF_8);// 追加模式写入字节数组到文件流fos.write(byteArray, 0, byteArray.length);// 写入换行符,以便下次写入数据时与上次数据分隔fos.write(System.lineSeparator().getBytes());// 关闭文件流fos.close();}public static ArrayList<Account> load(FileInputStream fis, File file) throws IOException, ClassNotFoundException {ArrayList<Account> accounts;if (file.length() == 0) {fis.close();accounts = new ArrayList<>();} else {// 创建一个ByteArrayOutputStream来存储读取的数据ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// 创建一个字节数组用于临时存储读取的数据byte[] buffer = new byte[1024];int bytesRead;// 从文件流中读取数据并存储到ByteArrayOutputStream中while ((bytesRead = fis.read(buffer)) != -1) {byteArrayOutputStream.write(buffer, 0, bytesRead);}// 将ByteArrayOutputStream中的数据转换为字节数组byte[] byteArray = byteArrayOutputStream.toByteArray();// 将字节数组转换为字符串,并指定编码方式为UTF-8String serializedData = new String(byteArray, StandardCharsets.UTF_8);// 将字符串转换为 ArrayList<Account>accounts = deserializeFromString(serializedData);// 关闭文件流fis.close();}return accounts;}// 将 ArrayList<Account> 序列化为字符串private static String serializeToString(ArrayList<Account> accounts) {// 在这里实现序列化逻辑,将 ArrayList<Account> 转换为字符串return accounts.toString();}// 将字符串反序列化为 ArrayList<Account>private static ArrayList<Account> deserializeFromString(String serializedData) {// 在这里实现反序列化逻辑,将字符串转换为 ArrayList<Account>ArrayList<Account> accounts = new ArrayList<>();// 你的反序列化逻辑return accounts;}public static void Accounts(ArrayList<Account> accounts) throws IOException {String filePath = "D:\\ATM1.txt";FileOutputStream fos = new FileOutputStream(new File(filePath));addAccount(accounts, fos);}public static ArrayList<Account> loadAccounts() throws IOException, ClassNotFoundException {String filePath = "D:\\ATM1.txt";File file = new File(filePath);if (!file.exists()) {file.createNewFile();}// 使用追加模式创建文件输出流FileOutputStream fos = new FileOutputStream(file, true);FileInputStream fis = new FileInputStream(file);return load(fis, file);}}