Java IO流 之 实现数据库

package com.database;

import java.io.*;
import java.util.*;

public class TestDatabase {
	
	public static void testAdd() {
		try {
			DatabaseStore ds = DatabaseStore.openDatabase("test.db");

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(baos);
			dos.writeInt(30);
			dos.writeBoolean(true);
			dos.writeDouble(100.5);
			dos.writeUTF("李军");

			ds.addRecord(baos.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void testUpdate() {
		try {
			DatabaseStore ds = DatabaseStore.openDatabase("test.db");

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(baos);
			dos.writeInt(30);
			dos.writeBoolean(true);
			dos.writeDouble(200.5);
			dos.writeUTF("张山");

			ds.setRecord(2, baos.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void testDelete() {
		DatabaseStore ds = DatabaseStore.openDatabase("test.db");

		ds.deleteRecord(3);

	}

	public static void testGet() {
		DatabaseStore ds = DatabaseStore.openDatabase("test.db");
		Iterator<Record> iter = ds.getRecords();
		try {
			while (iter.hasNext()) {
				Record record = iter.next();
				ByteArrayInputStream bais = new ByteArrayInputStream(record.getData());
				DataInputStream dis = new DataInputStream(bais);
				System.out.println(record.getId() + "," + dis.readInt() + "," + dis.readBoolean() + "," + dis.readDouble() + "," + dis.readUTF());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		testDelete();
		testGet();
	}
}

class Record implements Serializable {
	private int id;
	private byte[] data;

	public Record(int id, byte[] data) {
		super();
		this.id = id;
		this.data = data;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

}

class DatabaseStore implements Serializable {
	private static String databasePath;
	private static DatabaseStore ds = null;
	private int id;//自动增长的 id
	private List<Record> recordList;

	private DatabaseStore() {
		recordList = new ArrayList<Record>();
	}

	public static DatabaseStore openDatabase(String databaseName) {
		databasePath = System.getProperty("user.dir") + "/" + databaseName;
		File file = new File(databasePath);
		if (!file.exists()) {
			ds = new DatabaseStore();
			writeObject();
		}
		ds = readObject();
		return ds;
	}

	public boolean addRecord(byte[] data) {
		ds.id++;
		ds.recordList.add(new Record(ds.id, data));
		return writeObject();
	}

	public boolean setRecord(int id, byte[] data) {
		for (int i = 0; i < ds.recordList.size(); i++) {
			Record record = ds.recordList.get(i);
			if (record.getId() == id) {
				record.setData(data);
				return writeObject();
			}
		}
		return false;
	}

	public Iterator<Record> getRecords() {
		return ds.recordList.iterator();
	}

	public boolean deleteRecord(int id) {
		for (int i = 0; i < ds.recordList.size(); i++) {
			Record record = ds.recordList.get(i);
			if (record.getId() == id) {
				ds.recordList.remove(i);
				return writeObject();
			}
		}
		return false;
	}

	public static boolean writeObject() {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(new File(databasePath)));
			oos.writeObject(ds);
			oos.flush();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	public static DatabaseStore readObject() {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(new File(databasePath)));
			ds = (DatabaseStore) ois.readObject();
			return ds;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值