现在用的手机用了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>
}
源码下载
http://download.csdn.net/source/2763205
<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;">邮件联系
[email protected](qq常年不在线,邮件联系)</span> 朱晓 。相互交流 谢谢</span><br>