Java IO流对象流实操

文章介绍了Java中的Account类,实现了Serializable接口,以及IO类如何处理ArrayList的序列化和反序列化,包括使用FileOutputStream和FileInputStream进行文件操作。
摘要由CSDN通过智能技术生成

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;
    }
    @Override
    public 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-8
        byte[] 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-8
            String 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);
    }

}
  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值