Java IO流 之 实现数据库

http://www.verejava.com/?id=16994748096921

package com.database;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;

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();
	}
}





package com.database;

import java.io.Serializable;

public 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;
	}
	
	
}





package com.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public 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;
	}
}


http://www.verejava.com/?id=16994748096921

posted @ 2018-06-26 08:01  verejava  阅读(234)  评论(0编辑  收藏  举报