📄 phonebook.java
字号:
/**
* @(#)PhoneBook.java
* Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
* @version 1.0, 10/05/2004
* @author 饶荣庆
* @author
*/
package com.j2me.phonebook;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
/**
*PhoneBook是用来对电话薄操作的主类
*/
public class PhoneBook
{
private RecordStore rs = null;
private RecordEnumeration re = null;
private Friend friend = null;
public PhoneBook()
{
try
{
rs = RecordStore.openRecordStore("PhoneBook", true); //创建一个名为PhoneBook的记录
}
catch(Exception e)
{
}
}
/**
*添加联系人
*/
public void addFriend(int id, String name, String phone, String eMail)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(id);
dos.writeUTF(name);
dos.writeUTF(phone);
dos.writeUTF(eMail);
byte rec[] = baos.toByteArray();
baos.close();
dos.close();
try
{
rs.addRecord(rec, 0, rec.length);
}
catch(Exception e)
{
//System.out.println(e);
}
}
catch(Exception e)
{
//System.out.println(e);
}
}
/**
*删除联系人
*/
public void deleteFriend(int id)
{
int recordID = getRecordIDByID(id);
try
{
rs.deleteRecord(recordID);
}
catch(Exception e)
{
}
}
/**
*修改联系人信息
*/
public void modifyFriend(int id, String name, String phone, String eMail)
{
int recordID = getRecordIDByID(id);
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(id);
dos.writeUTF(name);
dos.writeUTF(phone);
dos.writeUTF(eMail);
byte rec[] = baos.toByteArray();
baos.close();
dos.close();
try
{
rs.setRecord(recordID, rec, 0, rec.length);
}
catch(Exception e)
{
//System.out.println(e);
}
}
catch(Exception e)
{
//System.out.println(e);
}
}
/**
*得到用户id
*/
public int getRecordIDByID(int id)
{
//-1表示记录存储中尚未有记录
int _id = -1;
RecordFilter rf = new FriendFilter(id);
try
{
re = rs.enumerateRecords(rf, null, false);
if (re.hasNextElement())
{
_id = re.nextRecordId();
}
}
catch(Exception e)
{
}
return _id;
}
/**
*通过用户id得到联系人信息
*/
public Friend getFriend(int id)
{
Friend friend = null;
byte data[] = new byte[200];
int _id;
String _name;
String _phone;
String _eMail;
RecordFilter rf = new FriendFilter(id);
try
{
re = rs.enumerateRecords(rf, null, true);
if (re.hasNextElement())
{
data = re.nextRecord();
}
ByteArrayToRecord batr = new ByteArrayToRecord(data);
_id = batr.getID();
_name = batr.getName();
_phone = batr.getPhone();
_eMail = batr.getEmail();
friend = new Friend(_id, _name, _phone, _eMail);
}
catch(Exception e)
{
}
finally
{
if (re!=null)
{
re.destroy();
}
}
return friend;
}
/**
*通过用户name得到联系人信息
*/
public Friend getFriend(String name)
{
Friend friend = null;
byte data[] = new byte[200];
int _id;
String _name;
String _phone;
String _eMail;
RecordFilter rf = new FriendFilter(name);
try
{
re = rs.enumerateRecords(rf, null, true);
if (re.hasNextElement())
{
data = re.nextRecord();
}
ByteArrayToRecord batr = new ByteArrayToRecord(data); //通过类ByteArrayToRecord获取用户信息
_id = batr.getID();
_name = batr.getName();
_phone = batr.getPhone();
_eMail = batr.getEmail();
friend = new Friend(_id, _name, _phone, _eMail);
}
catch(Exception e)
{
}
finally
{
if (re!=null)
{
re.destroy();
}
}
return friend;
}
/**
*获取所有用户
*/
public Vector getAllFriends(int sortBy)
{
Vector vectorFriends = new Vector();
Friend friend;
int _id;
String _name;
String _phone;
String _eMail;
RecordComparator rc = new FriendComparator(sortBy);
try
{
re = rs.enumerateRecords(null, rc, true);
while (re.hasNextElement())
{
ByteArrayToRecord batr = new ByteArrayToRecord(re.nextRecord());
_id = batr.getID();
_name = batr.getName();
_phone = batr.getPhone();
_eMail = batr.getEmail();
friend = new Friend(_id, _name, _phone, _eMail);
vectorFriends.addElement(friend);
}
}
catch(Exception e)
{
}
finally
{
if (re!=null)
{
re.destroy();
}
}
return vectorFriends;
}
/**
*close rs
*/
public void closeRS()
{
try
{
rs.closeRecordStore();
}
catch(Exception e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -