第一步:创建CostomerDao类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;//客户实现类
public class CustomerDao{//clients --客户private File clients = new File("C:\Users\54166\Desktop\");/*** 写--序列化* Map<String 学号,Costomer 客户信息>*/
public void update(Map<String,Customer>data) {/*** 1.创建序列化流对象* FileoutputStream(File file,false)* ObjectOutputStream(OutputStream out);*/try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(clients,false))) {/*** 2.写入数据* 把map集合写进去*/oos.writeObject(data);}catch(IOException e) {e.printStackTrace();}
}
public Map<String,Customer>query(){//map是一个接口,不能new对象Map<String,Customer> data = new HashMap<String,Customer>();//证明是空文件,没有内容if(clients.length() == 0) {return data;}/*** 创建反序列化流* new FileInputStream(File file)* new ObjectInputStream(InputStream in)*/try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(clients))){ //读取数据data = (Map<String,Customer>)adObject();}catch(IOException e) {e.printStackTrace();}catch(ClassNotFoundException e) {e.printStackTrace();}return data;}
}
第二步:创建Customer类
import java.io.Serializable;public class Customer extends Person {//继承Person类private String customerId;//idprivate String phone;//客户电话//无参构造器public Customer() {}//有参构造器public Customer(String customerId,String phone) {this.customerId = customerId;this.phone = phone;}//此方法返回客户对象public static Customer builder() {return new Customer();}//返回一个客户id修改过的客户对象public Customer customerId(String customerId) {this.customerId = customerId;return new Customer();}//返回一个客户电话修改过或者赋值过的客户对象public Customer phone(String phone) {this.phone = phone;return new Customer();}@Overridepublic String toString() {return "用户信息 [编号 = "+this.customerId+",姓名="+getName()+",性别="+getSex()+",年龄="+getAge()+",手机号码="+this.phone+"]";}
}
第三步:创建Person类
import java.io.Serializable;public class Person implements Serializable{//实现Serializable接口/*** */private static final long serialVersionUID = 1L;private String name;//名字private String sex;//性别private int age;//年龄private String pwd;//密码public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public void setAge(int age) {this.age = age;}}
最后一步创建测试类
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;public class CustmoerDaotest {public static void main(String[] args) {CustomerDao ct = new CustomerDao();//写Map<String,Customer>map = new HashMap<String,Customer>();Customer cust = new Customer("01","11111111111");cust.setName("张三");//姓名cust.setAge(18);//年龄cust.setSex("男");//性别map.put("1011",cust);ct.update(map);//读Map<String,Customer> du = ct.query();/*** 数据存在了map里面我们遍历map就可以了*/Iterator<Entry<String,Customer>>iterator = du.entrySet().iterator();while(iterator.hasNext()) {Entry<String,Customer>next = ();//获取map的keyString key = Key();//获取map的valueCustomer value = Value();System.out.println(key+"----"+value);}}}
本文发布于:2024-01-31 12:10:24,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170667422628421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |