Back up and restore the mobile phone address book using j2-based Storage

Source: Internet
Author: User

The current mobile phone has been used for four and a half years. It has been broken for n times, but it has never been broken. Recently I want to change to a Windows Mobile Phone. Before changing the phone number, let's make a try on this old mobile phone. The old mobile phone only supports j2_m2. Use it to back up the address book. Follow these steps to back up your phone address book:

1. Save the address book content to a string through the PIM-related function of j2_m2.

String name = "";
String Tel = "";
String Dis = "";
Pim PIM = Pim. getinstance ();
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. Save the address book through the fileconnection of j2_m2.

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

In this way, the address book content has been saved to a.txt and saved to the computer.

In this case, if you change your mobile phone, you can use the.txt file to restore the address book.

When restoring, you only need to read the backup file through fileconnection and restore it to the mobile phone address book. The main code is as follows:

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 [1, 1024*5];
Int length = is. Read (B, 0, 1024*5 );
Is. Close ();
FC. Close ();
If (length> 0)
{
S = new string (B, * 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) Pim. openpimlist (PIM. contact_list, Pim. read_write );
Pimitem item = NULL;
Item = (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 ());
}

Source code download http://download.csdn.net/source/2763205

If you find anything unreasonable and need improvement, mail contact 328452421@qq.com (qq perennial not online, mail contact) Zhu Xiao. Exchange with each other. Thank you.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.