- OutputStreamWriter writer = ...(此处略去 )
- VCardComposer composer = new VCardComposer();
- //create a contact
- ContactStruct contact1 = new ContactStruct();
- contact1.name = "张三";
- contact1.company = "海辉集团";
- contact1.addPhone("+123456789", String.valueOf(Contacts.Phones.TYPE_MOBILE), null);
- //create vCard representation
- String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT);
- //write vCard to the output stream
- writer.write(vcardString);
- writer.write("/n"); //add empty lines between contacts
- // repeat for other contacts
- ...
- writer.close();
- Reading vCard
- VCardParser parser = new VCardParser();
- VDataBuilder builder = new VDataBuilder();
- //read whole file to string
- //(FileUtils are part of Commons IO library, do it by hand if you don't use it)
- String vcardString = FileUtils.readFileToString(file, "UTF-8");
- //parse the string
- boolean parsed = parser.parse(vcardString, "UTF-8", builder);
- if (!parsed) {
- throw new VCardException("Could not parse vCard file: " + file);
- }
- //get all parsed contacts
- List<VNode> pimContacts = builder.vNodeList;
- //do something for all the contacts
- for (VNode contact : pimContacts) {
- ArrayList<PropertyNode> props = contact.propList;
- //contact name - FN property
- String name = null;
- for (PropertyNode prop : props) {
- if ("FN".equals(prop.propName)) {
- name = prop.propValue;
- //we have the name now
- break;
- }
- }
- //similarly for other properties (FN, ORG, TEL, etc)
- OutputStreamWriter writer = ...(此处略去 )
- VCardComposer composer = new VCardComposer();
- //create a contact
- ContactStruct contact1 = new ContactStruct();
- contact1.name = "张三";
- contact1.company = "海辉集团";
- contact1.addPhone("+123456789", String.valueOf(Contacts.Phones.TYPE_MOBILE), null);
- //create vCard representation
- String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT);
- //write vCard to the output stream
- writer.write(vcardString);
- writer.write("/n"); //add empty lines between contacts
- // repeat for other contacts
- ...
- writer.close();
- Reading vCard
- VCardParser parser = new VCardParser();
- VDataBuilder builder = new VDataBuilder();
- //read whole file to string
- //(FileUtils are part of Commons IO library, do it by hand if you don't use it)
- String vcardString = FileUtils.readFileToString(file, "UTF-8");
- //parse the string
- boolean parsed = parser.parse(vcardString, "UTF-8", builder);
- if (!parsed) {
- throw new VCardException("Could not parse vCard file: " + file);
- }
- //get all parsed contacts
- List<VNode> pimContacts = builder.vNodeList;
- //do something for all the contacts
- for (VNode contact : pimContacts) {
- ArrayList<PropertyNode> props = contact.propList;
- //contact name - FN property
- String name = null;
- for (PropertyNode prop : props) {
- if ("FN".equals(prop.propName)) {
- name = prop.propValue;
- //we have the name now
- break;
- }
- }
- //similarly for other properties (FN, ORG