📄 index.java
字号:
package com.hziee.phone_book.model;
import java.io.*;
//索引类
public class Index {
private String key;//关键字
private int recordID;//记录ID
public Index(String key, int recordID)
{
this.key = key;
this.recordID = recordID;
}
private Index()
{
}
/**
* @return 返回 key.
*/
public String getKey()
{
return key;
}
/**
* @param 设置 key.
*/
public void setKey(String key)
{
this.key = key;
}
/**
* @return 返回 recordID.
*/
public int getRecordID()
{
return recordID;
}
/**
* @param 设置 recordID .
*/
public void setRecordID(int recordID)
{
this.recordID = recordID;
}
/**把内存中的数据读到字节数组中*/
public byte[] serialize() throws IOException
{
/** 在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,
然后利用ByteArrayOutputStream和
ByteArrayInputStream的实例向数组中写入或读出byte型数据*/
//字节流转化
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(key);
dos.writeInt(recordID);
baos.close();
dos.close();
return baos.toByteArray();
}
/**又把字节数组中的字节以流的形式读出,实现了对同一个字节数组的操作.*/
public static Index deserialize(byte[] data) throws IOException
{//读出字节流
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
//创建索引实例
Index index = new Index();
//当前实例读取的属性
index.key = dis.readUTF();
index.recordID = dis.readInt();
bais.close();
dis.close();
return index;
}
public static boolean matches(byte[] data, String key,int type) throws IOException
{
//将data字节流转换为输入流
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
if(type == 101)
{
return (dis.readUTF()).equals(key);
}
else if(type == 102)
{
return (dis.readUTF()).startsWith(key);
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -