CRUD操作
本篇所用到的技术:
- 接口的定义,实现类
- 序列化
- 利用对象字节流-包装流,对数据进行增删改查到指定文件中,和对文件进行读取
Phone实体类
package stu.aistar.day13.project.entity;
import java.io.Serializable;
public class Phone implements Serializable {
private int id;
private String name;
private double price;
public Phone() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Phone(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Phone{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", price=").append(price);
sb.append('}');
return sb.toString();
}
}
IPhoneDao接口(对数据的操作)
package stu.aistar.day13.project.dao;
import stu.aistar.day13.project.entity.Phone;
import java.util.List;
public interface IPhoneDao {
List<Phone> findAll();
void delById(Integer id);
void save(Phone phone);
void updateById(Integer id);
}
PhoneDaoImpl实现类
package stu.aistar.day13.project.dao.impl;
import stu.aistar.day13.project.dao.IPhoneDao;
import stu.aistar.day13.project.entity.Phone;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 本类用于演示:
*/
public class PhoneDaoImpl implements IPhoneDao {
private String path = "src/stu/aistar/day13/project/data/phone.txt";
@Override
public List<Phone> findAll() {
List<Phone> list = new ArrayList<>(); //定义一个数组用来存储查询出来的信息
File file = new File(path);
if (!file.exists() || file.length()==0){
System.out.println("文件不存在!!!!!!!!!!!!!!!!!!");
return null;
}
try(ObjectInputStream in =new ObjectInputStream(new FileInputStream(path))){
list = (List<Phone>) in.readObject(); //从文件中读取数据 存放到list集合中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list; //返回list
}
@Override
public void delById(Integer id) {
List<Phone> list = findAll(); //先把所有的产品查询出来 以便后序操作(del...)
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))) {
boolean flag = false; //用来记录 想要删除的id对应的产品是否存在
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id){
list.remove(i);
i--;
flag = true; //存在想要删除的产品
}
}
if (!flag){ //flag = false的时候进入这里 证明产品不存在
System.out.println("您想要删除的产品不存在!");
}
out.writeObject(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void save(Phone phone) {
File file =new File(path);
if (!file.exists()){ //第一次存产品的时候进入 -> 第一次该文件还不存在 //多余 写入文件自动创建
try {
file.createNewFile(); //创建文件
} catch (IOException e) {
e.printStackTrace();
}
List<Phone> list = new ArrayList<>(); //创建集合
list.add(phone); //存入该产品
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))) {
out.writeObject(list);//写入文件(提示:每一次写文件 系统都是将该文件清空 然后再存入你写进去的文件)
System.out.println("保存成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(file.length()==0){ //文件存在 但是文件为空的状态下
List<Phone> list = new ArrayList<>(); //需要创建一个集合 以便后序存储
list.add(phone); //将产品存进集合
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))) {
out.writeObject(list); //写入文件
System.out.println("保存成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else { //此处为文件存在 且文件不为空的情况下
List<Phone> oldList = findAll(); //先把文件中的数据查询出来 然后加入新产品
oldList.add(phone);
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))) {
out.writeObject(oldList); //存进文件里
System.out.println("保存成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void updateById(Integer id) {
List<Phone> list = findAll();
Phone phone = null;
Scanner sc = new Scanner(System.in);
System.out.println("您要修改的是"+id+"号产品:");
for (int i = 0; i < list.size(); i++) { //找到你想要修改的产品的id对应的下标
if (list.get(i).getId() == id){
phone = list.get(i); //把下标对应的产品拿出来
System.out.print("请修改它的名字:");
phone.setName(sc.nextLine()); //修改为新的名字
System.out.print("请修改它的价格:");
phone.setPrice(sc.nextDouble()); //修改为新的价格
list.remove(i); //把原本的那个数据删除
list.add(phone); //存进你修改过后的原产品
break; //break 的原因是 你已经找到了该产品
}
}
System.out.println("修改成功!");
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))){
out.writeObject(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面就是一些测试。。。