随机获取文件对象、序列化与反序列化实现携号转网
一、题目
方法:序列化与反序列化
序列化 | 反序列化 |
---|---|
ObjectOutputStream | ObjectOutputStream |
二、代码实现
1.移动公司类
package Access;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class MovePhone {
//构造方法
public MovePhone() {
}
public void cmRead() throws IOException {
System.out.println("==========欢迎来到移动大厅==========");
File file = new File("src/Access/cm.txt");
//获取到随机文件的对象(UTF-8编码下的读取)
RandomAccessFile raf = new RandomAccessFile(file,"r");
//获取键盘扫描器
Scanner input = new Scanner(System.in);
//获取用户信息
System.out.println("请输入你的姓名:");
String name = input.next();
System.out.println("请输入你的电话:");
String phone = input.next();
System.out.println("请输入你的身份证:");
String card = input.next();
//标签
boolean flag = true;
//循环读取数据
while(raf.read() != -1) {
//讲指针的位置更改为当前未被读取到的位置
raf.seek(raf.getFilePointer()-1);
//读取姓名
byte[] b1 = new byte[12];
raf.read(b1);
String sName = new String(b1);
//读取电话
byte[] b2 = new byte[11];
raf.read(b2);
String sPhone = new String(b2);
//读取身份证
byte[] b3 = new byte[18];
raf.read(b3);
String sCord = new String(b3);
//读取金额
byte[] b4 = new byte[6];
raf.read(b4);
String sMoney = new String(b4);
//判断是否是指定用户
if(name.equals(sName.trim()) && phone.equals(sPhone) && card.equals(sCord)) {
System.out.println("匹配到指定用户");
//判断是否欠费
if(!"-".equals(""+sMoney.charAt(0))) {
System.out.println("尚未欠费");
File fil = new File("src/Access/data.txt");
ObjectOutputStream oos = null;
//获取序列化的对象
if(fil.length()<1) {
oos = new ObjectOutputStream(new FileOutputStream(fil,true));
}else {
oos = new Sub(new FileOutputStream(fil,true));
}
//把对象写入到文件中
User user = new User(sName,sPhone,sCord,sMoney);
oos.writeObject(user);
//释放资源
oos.close();
System.out.println("序列化成功!");
}else {
System.out.println("该用户已经欠费,无法申请转网");
}
flag = false;
break;
}
raf.skipBytes(2);
}
if(flag) {
System.out.println("未能查询到用户信息");
}else {
ConnectedPhone c = new ConnectedPhone();
c.cuWrite(name, phone, card);
}
//释放资源
raf.close();
input.close();
}
}
2.联通公司类
package AccessHomeWork;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ConnectedPhone {
//构造方法
public ConnectedPhone() {
}
//反序列化和写入的方法
public void cuWrite(String name,String phone,String card) {
System.out.println("==========欢迎来到联通大厅==========");
File file = new File("src/AccessHomeWork/data.txt");
ObjectInputStream ois = null;
FileOutputStream fos = null;
//反序列化
try {
try{
ois = new ObjectInputStream(new FileInputStream(file));
Object obj = null;
while((obj = ois.readObject()) != null) {
Person person = (Person)obj;
if(name.equals(person.getName().trim()) && phone.equals(person.getPhone()) && card.equals(person.getNum())) {
//将数据写入到cu.txt文件中
fos = new FileOutputStream("src/AccessHomeWork/cu.txt",true);
String str = person.getName() + person.getPhone() + person.getNum() + person.getCost() + "\r\n";
fos.write(str.getBytes());
System.out.println("办理成功");
break;
}
}
}catch(EOFException e) {
System.out.println("暂无用户信息");
System.out.println("反序列化成功");
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(ois!=null) {
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.Sub类
package AccessHomeWork;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/*
* 序列化的时候防止头标报错
*/
public class Sub extends ObjectOutputStream {
public Sub(OutputStream out) throws IOException{
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
}
}
4.Person类
package AccessHomeWork;
import java.io.Serializable;
public class Person implements Serializable{
private String name;//姓名
private String phone;//电话号码
private String num;//身份证号
private String cost;//话费余额
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, String phone, String num, String cost) {
super();
this.name = name;
this.phone = phone;
this.num = num;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
@Override
public String toString() {
return "Person [name=" + name + ", phone=" + phone + ", num=" + num + ", cost=" + cost + "]";
}
}
4.对应的文件
4.1 cost.txt
攀攀 15312341234421421198912245299-136.5
湛湛帅 15312341234421421198912245299+106.5
苏苏最帅15312341234421421198912245299-126.5
欧阳锋 15312341234421421122112245299+336.5
铁木真 15312341234421421100112245299+536.5
郭靖 15312341234421421198912245299-106.5
杨过 15312341234421421198912245299-236.5
4.2 data.txt(空)
用来接收序列化的信息
4.3 cu.txt(空)
用来接收反序列化的信息
5.运行结果展示
若客户不欠费就能办理转网成功
若欠费就不成功
本期分享到此结束