j2me实现手机通讯录的备份与还原

现在用的手机用了4年半了,其中摔过n次,但是从没坏过,现在还活的好好的。最近想换个windows系统版本的手机。换手机前,先再折腾折腾这个老手机吧,老手机手机只支持j2me。就用它备份下通讯录吧。备份手机通讯录的步骤分以下几步

1.通过j2me的pim相关函数把通讯录内容存入字符串中

String name="";<br>
String tel="";<br>
String dis="";<br>
PIM pim = PIM.getInstance();<br>
ContactList contactList = null;<br>
Enumeration em = null;<br>
try {<br>
contactList = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);<br>
em=contactList.items();<br>
while(em.hasMoreElements())<br>
{<br>
Contact contact=(Contact)em.nextElement();<br>
name=contact.getString(Contact.FORMATTED_NAME, 0);<br>
dis=dis+name+",";<br>
tel=contact.getString(Contact.TEL, 0);<br>
dis=dis+tel+"/n";<br>
}<br>
} catch (PIMException ex) {<br>
ex.printStackTrace();<br>
}

2.通过j2me的FileConnection 完成通讯录的保存工作

FileConnection fc;<br>
try {<br>
String elem = null;<br>
Enumeration e = FileSystemRegistry.listRoots();<br>
while (e.hasMoreElements()) {<br>
elem = e.nextElement().toString();<br>
}<br>
System.out.println("::"+elem);<br>
//fc = (FileConnection) Connector.open("<a>file://localhost/</a>" + elem+"/a.txt");<br>
fc = (FileConnection) Connector.open("<a>file://localhost/c/mobile/video/a.txt</a>");<br>
if (!fc.exists()) {<br>
fc.create();<br>
OutputStream is = fc.openOutputStream();<br>
is.write(dis.getBytes("UTF-8"), 0, dis.getBytes("UTF-8").length);<br>
is.flush();<br>
is.close();<br>
}<br>
} catch (Exception e) {<br>
t.setString(e.toString());<br>
}

这样通讯录的内容已经保存到 a.txt ,把它存入电脑就完成了保存工作

这样如果换了新手机就直接可以用a.txt 文件来还原通讯录了

还原的时候只需通过FileConnection 读取备份的文件,还原到手机通讯录就ok了 主要代码如下

FileConnection fc;<br>
String s="";<br>
try {<br>
String elem = null;<br>
Enumeration e = FileSystemRegistry.listRoots();<br>
while (e.hasMoreElements()) {<br>
elem = e.nextElement().toString();<br>
}<br>
System.out.println("::"+elem);<br>
//fc = (FileConnection) Connector.open("<a>file://localhost/</a>" + elem+"/a.txt");<br>
fc = (FileConnection) Connector.open("<a>file://localhost/c/mobile/video/a.txt</a>");<br>
if (!fc.exists())<br>
{<br>
throw new IOException("File does not exists");<br>
}<br>
if (fc.exists())<br>
{<br>
InputStream is = fc.openInputStream();<br>
byte[] b = new byte[1024*5];<br>
int length = is.read(b, 0, 1024*5);<br>
is.close();<br>
fc.close();<br>
if(length > 0)<br>
{<br>
s=new String(b, 0,1024*5,"UTF-8");<br>
String [] str=split(s,"/n");<br>
for(int i=0;i<str.length;i++)<br>
{<br>
if(str[i].indexOf(",")>=0)<br>
{<br>
String [] NameAndTel=split(str[i],",");<br>
PIM pim = PIM.getInstance();<br>
ContactList contactList = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);<br>
PIMItem item = null;<br>
item = ((ContactList) contactList).createContact ();<br>
item.addString(Contact.FORMATTED_NAME, PIMItem.ATTR_NONE, NameAndTel[0]);<br>
item.addString (Contact.TEL, PIMItem.ATTR_NONE, NameAndTel[1]);<br>
item.commit();<br>
}<br>
}

}<br>
}<br>
} catch (Exception e) {<br>
t.setString(e.toString());<br>
}

源码下载 [url=http://download.csdn.net/source/2763205]http://download.csdn.net/source/2763205[/url]

<br>

<br>

<span style="font-family: Arial; font-size: 14px; line-height: 26px;">如果你发现有什么不合理的,需要改进的地方,<span style="font-family: 'lucida Grande',Verdana; font-size: 14px; line-height: 23px;">邮件联系328452421@qq.com(qq常年不在线,邮件联系)</span> 朱晓 。相互交流 谢谢</span><br>
思路很简单,通过pim读取通讯录保存到文件中完成备份工作还原时再通过这个文件还原通讯录 http://blog.csdn.net/xiaoxiao108/archive/2010/10/17/5947240.aspx 如果你发现有什么不合理的,需要改进的地方,联系328452421@qq.com 朱晓 (泰山学院)。相互交流 谢谢 现在用的手机用了4年半了,其中摔过n次,但是从没坏过,现在还活的好好的。最近想换个windows系统版本的手机。换手机前,先再折腾折腾这个老手机吧,老手机手机只支持j2me。就用它备份通讯录吧。备份手机通讯录的步骤分以下几步 1.通过j2me的pim相关函数把通讯录内容存入字符串中 String name=""; String tel=""; String dis=""; PIM pim = PIM.getInstance(); ContactList contactList = null; Enumeration em = null; try { contactList = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); em=contactList.items(); while(em.hasMoreElements()) { Contact contact=(Contact)em.nextElement(); name=contact.getString(Contact.FORMATTED_NAME, 0); dis=dis+name+","; tel=contact.getString(Contact.TEL, 0); dis=dis+tel+"\n"; } } catch (PIMException ex) { ex.printStackTrace(); } 2.通过j2me的FileConnection 完成通讯录的保存工作 FileConnection fc; try { String elem = null; Enumeration e = FileSystemRegistry.listRoots(); while (e.hasMoreElements()) { elem = e.nextElement().toString(); } System.out.println("::"+elem); //fc = (FileConnection) Connector.open("file://localhost/" + elem+"/a.txt"); fc = (FileConnection) Connector.open("file://localhost/c/mobile/video/a.txt"); if (!fc.exists()) { fc.create(); OutputStream is = fc.openOutputStream(); is.write(dis.getBytes("UTF-8"), 0, dis.getBytes("UTF-8").length); is.flush(); is.close(); } } catch (Exception e) { t.setString(e.toString()); } 这样通讯录的内容已经保存到 a.txt ,把它存入电脑就完成了保存工作 这样如果换了新手机就直接可以用a.txt 文件来还原通讯录还原的时候只需通过FileConnection 读取备份的文件,还原手机通讯录就ok了 主要代码如下 FileConnection fc; String s=""; try { String elem = null; Enumeration e = FileSystemRegistry.listRoots(); while (e.hasMoreElements()) { elem = e.nextElement().toString(); } System.out.println("::"+elem); //fc = (FileConnection) Connector.open("file://localhost/" + elem+"/a.txt"); fc = (FileConnection) Connector.open("file://localhost/c/mobile/video/a.txt"); if (!fc.exists()) { throw new IOException("File does not exists"); } if (fc.exists()) { InputStream is = fc.openInputStream(); byte[] b = new byte[1024*5]; int length = is.read(b, 0, 1024*5); is.close(); fc.close(); if(length > 0) { s=new String(b, 0,1024*5,"UTF-8"); String [] str=split(s,"\n"); for(int i=0;i<str.length;i++) { if(str[i].indexOf(",")>=0) { String [] NameAndTel=split(str[i],","); PIM pim = PIM.getInstance(); ContactList contactList = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); PIMItem item = null; item = ((ContactList) contactList).createContact (); item.addString(Contact.FORMATTED_NAME, PIMItem.ATTR_NONE, NameAndTel[0]); item.addString (Contact.TEL, PIMItem.ATTR_NONE, NameAndTel[1]); item.commit(); } } } } } catch (Exception e) { t.setString(e.toString()); } 源码下载 http://download.csdn.net/source/2763205
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值