当前位置: 首页 > news >正文

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);}}
http://www.lryc.cn/news/336635.html

相关文章:

  • Mapbox教程:一个简单Demo
  • 看AI赋能数智化 | Gooxi AI服务器闪耀CITE 2024
  • 大话设计模式——21.中介者模式(Mediator Pattern)
  • Linux 计算机网络
  • bash脚本中‘-b -u -p’‘$# -eq’‘#!/bin/bash’‘sed -i “s/\r//“ $1’的用法说明
  • 【人工智能】Gitee AI 天数智芯有奖体验开源AI模型,一定能有所收货,快来体验吧
  • Ceph学习 -8.认证管理-用户基础
  • 大创项目推荐 深度学习+opencv+python实现昆虫识别 -图像识别 昆虫识别
  • Python小工具提升工作效率【附完整版,可下载word】
  • k8s-Pod
  • 智能网络新纪元:机器学习赋能未来计算机网络高速发展
  • 大数据之kafka应用
  • 嵌入式操作教程_数字信号处理_音频编解码:3-6 AAC音频解码实验
  • 【从浅学到熟知Linux】冯诺依曼体系结构及进程概念详谈!
  • word转pdf(前后端最全保姆级别)
  • 深度学习简介
  • 深入探索:使用C++构建顶级性能的网络服务器
  • SpringBoot学习笔记四
  • 雄安建博会:中矿雄安新区的总部开工建设
  • 蒙特卡洛方法【强化学习】
  • 构建第一个ArkTS之声明式UI描述
  • pytest教程-25-生成覆盖率报告插件-pytest-cov
  • 特征工程总结
  • JUC并发编程2(高并发,AQS)
  • Golang 为什么需要用反射
  • 【Linux的进程篇章 - 进程终止和进程等待的理解】
  • 《策略模式(极简c++)》
  • Python向文件里写入数据
  • 【网站项目】校园订餐小程序
  • vue-指令v-for