Java parsing vcf format file import and Export

Source: Internet
Author: User
Tags vcard

Package Com.yfli.test;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.io.unsupportedencodingexception;import Java.util.arraylist;import Java.util.list;import Java.util.regex.matcher;import Java.util.regex.Pattern;import Import and export of Org.junit.test;import org.omg.corba.systemexception;import com.yfli.entity.addressbean;/** *. vcf format File * @author Yfli * */public class Importvcftest {@Testpublic void importvcf () {FileInputStream fis = null;try {fis = new fileinputstre AM (New File ("SRC/IMPORT_CONTACTS.VCF")); list<addressbean> Addressbeans = this.importvcffilecontact (FIS); System.out.println (Addressbeans.size ()); for (Addressbean Addressbean:addressbeans) {System.out.println ("Turename: "+ addressbean.gettruename ()); System.out.println ("Mobile: "+ addressbean.getmobile ()); System.out.println ("Workmobile:" + addressbean.getworkmobile ()); System.out.println ("Email:" + addressbean.getemail ()); System.out.println ("--------------------------------");}} catch (FileNotFoundException e) {e.printstacktrace ();}} @Testpublic void exportvcf () {try {list<addressbean> Addressbeans = new arraylist<addressbean> (); Addressbean Addressbean = new Addressbean () addressbean.settruename ("Zhangjie"); Addressbean.setmobile ("18255963695 "); Addressbeans.add (addressbean); Addressbean = new Addressbean (); Addressbean.settruename (" Zhang San "); Addressbean.setmobile ("15255963695"); Addressbeans.add (Addressbean); File File = new file ("SRC/EXPORT_CONTACTS.VCF"), if (File.exists ()) {file.createnewfile ();} BufferedWriter reader = new BufferedWriter (new PrintWriter (file)), for (Addressbean Bean:addressbeans) {reader.write (" Begin:vcard "), Reader.write (" \ r \ n "), Reader.write (" version:2.1 "), Reader.write (" \ r \ n "); Charset=utf-8; Encoding=quoted-priNtable: "+ this.qpencodeing (Bean.gettruename ()) +"; "); Reader.write ("\ r \ n"); if (""! = Bean.getmobile () && bean.getmobile ()! = null) {reader.write ("TEL; CELL: "+ bean.getmobile () +" "); Reader.write (" \ r \ n ");} if (""! = Bean.getworkmobile () && bean.getworkmobile ()! = null) {reader.write ("TEL; Work: "+ bean.getworkmobile () +" "); Reader.write (" \ r \ n ");} if (""! = Bean.gettelephone () && bean.gettelephone ()! = null) {reader.write ("TEL; HOME: "+ bean.gettelephone () +" "); Reader.write (" \ r \ n ");} if (""! = Bean.getemail () && bean.getemail ()! = null) {Reader.write ("EMAIL:" + bean.getemail () + ""); Reader.write ( "\ r \ n");} Reader.write ("End:vcard"); Reader.write ("\ r \ n");} Reader.flush (); Reader.close ();} catch (Exception e) {e.printstacktrace ();}} /** * Import Contacts * @param in * @return * @throws systemexception */public static list<addressbean> importvcffilecontact (I Nputstream in) throws SystemException {list<addressbean> Addressbeans = new arraylist<addressbean> (); try{BufferedReader reader = new BufferedReader (new InputStreamReader (in)); StringBuffer bu = new StringBuffer (); String Line;while (line = importvcftest.nextline (reader))! = null) {Bu.append (line + "\ r \ n");} Pattern p = pattern.compile ("Begin:vcard (\\r\\n) ([\\s\\S\\r\\n\\.] *?) End:vcard "); Matcher m = P.matcher (Bu.tostring ()), while (M.find ()) {Addressbean is = new Addressbean (); String str = m.group (0); Pattern pn = Pattern.compile ("N; [\\s\\S\\r\\n\\.] *?) ([\\r\\n]);//name Matcher MN = Pn.matcher (m.group (0)), while (Mn.find ()) {String name = "", if (Mn.group (1). IndexOf (" Encoding=quoted-printable ") >-1) {name = Mn.group (1). substring (Mn.group (1). IndexOf (" encoding=quoted-printable: " + "encoding=quoted-printable:". Length ()); name = Name.substring (Name.indexof (":") + 1); if (Name.indexof (";") >-1) {name = name.substring (0, Name.indexof (";")); Be.settruename (importvcftest.qpdecoding (name));} else {be.settruename (importvcftest.qpdecoding (name));}} else {Pattern PNN = Pattern.compile ("charset= ([A-ZA-z0-9-]*?): "); Matcher mnn = Pnn.matcher (Mn.group (1)), while (Mnn.find ()) {name = Mn.group (1). substring (Mn.group (1). IndexOf (Mnn.group (0)) + mnn.group (0). Length ()); Be.settruename (name);}} String cell = ""; Pattern P1 = Pattern.compile ("TEL; CELL: ([\\s*\\d*\\s*\\d*\\s*\\d*]*?) ([\\r\\n]); Matcher m1 = P1.matcher (str); while (M1.find ()) {String Tel = m1.group (0); cell = Tel.substring ("TEL; CELL: ". Length ());} Be.setmobile (cell); String work = ""; Pattern P2 = pattern.compile ("TEL; work:\\d* ([\\s*\\d*\\s*\\d*\\s*\\d*]*?) ([\\r\\n]); Matcher m2 = p2.matcher (str), while (M2.find ()) {System.out.println ("Worktel:" + m2.group (0)), work = M2.group (0). Substri Ng (M2.group (0). IndexOf ("TEL; Work: ") +" TEL; Work: ". Length ());} Be.setworkmobile (work); String home = ""; Pattern p3 = pattern.compile ("TEL; HOME: ([\\s*\\d*\\s*\\d*\\s*\\d*]*?) ([\\r\\n]); Matcher m3 = p3.matcher (str); while (M3.find ()) {home = M3.group (0). Substring (m3.group (0). IndexOf ("TEL; HOME: ") +" TEL; HOME: ". Length ());} Be.settelephone (home); String email = ""; Pattern P4 = Pattern.compile ("\\w+ (\\.\\w+) *@\\w+ (\\.\\w+) +"); Matcher m4 = P4.matcher (str), while (M4.find ()) {email = m4.group (0);} Be.setemail (email); Addressbeans.add (BE);} Reader.close ();} catch (Exception e) {e.printstacktrace ();} return Addressbeans;} public static string nextline (BufferedReader reader) throws IOException {string line; String Nextline;do {line = Reader.readline (), if (line = = null) {return NULL,}} while (line.length () = = 0), while (LINE.ENDSW ITH ("=")) {line = line.substring (0, Line.length () – 1); line + = Reader.readline ();} Reader.mark (+); nextline = Reader.readline (); if (nextline! = null) && (nextline.length () > 0) && (( Nextline.charat (0) = = 0x20) | | (Nextline.charat (0) = = 0x09))) {line + = nextline.substring (1);} else {reader.reset ();} line = Line.trim (); return line;} /* decode */public static string qpdecoding (String str) {if (str = = null) {return "";} try {str = str.replaceall ("=\n", ""); byte[] bytes = str.getbytes ("Us-ascii"); for (int i = 0; I < bYtes.length; i++) {byte b = bytes[i];if (b! =) {Bytes[i] = b;} else {bytes[i] = 32;}} if (bytes = = null) {return "";} Bytearrayoutputstream buffer = new Bytearrayoutputstream (); for (int i = 0; i < bytes.length; i++) {int b = bytes[i];if (b = = ' = ') {try {int u = character.digit (char) bytes[++i], + int l = character.digit (char) bytes[++i], () if (U = =-1 | | l = =-1 ) {continue;} Buffer.write ((char) ((U << 4) + L)); catch (ArrayIndexOutOfBoundsException e) {e.printstacktrace ();}} else {buffer.write (b);}} return new String (Buffer.tobytearray (), "UTF-8");} catch (Exception e) {e.printstacktrace (); return "";}} /* * Encode */public static string qpencodeing (String str) {char[] encode = Str.tochararray (); StringBuffer sb = new StringBuffer (), for (int i = 0; i < encode.length; i++) {if (Encode[i] >= '! ') && (enc  Ode[i] <= ' ~ ') && (encode[i]! = ' = ') && (encode[i]! = ' \ n ')) {sb.append (encode[i]);} else if (Encode[i] = = ' = ') {sb.append ("=3d");} else if (EncoDe[i] = = ' \ n ') {sb.append ("\ n"),} else {StringBuffer sbother = new StringBuffer (); Sbother.append (Encode[i]); String ss = sbother.tostring (); byte[] buf = null;try {buf = ss.getbytes ("Utf-8");} catch (Unsupportedencodingexception e) {E.printstacktrace ();} if (buf.length = = 3) {for (int j = 0; J < 3; J + +) {String s16 = string.valueof (integer.tohexstring (Buf[j]));//extract Chinese characters 16 The last two bits of the binary byte, that is, the two bits after the =e8 equals sign,//Three Represents a Chinese character Char C16_6;char c16_7;if (S16.charat (6) >= && S16.charat (6) <= 122) {c16_6 = (char) (S16.charat (6)-32);} else {c16_6 = S16.charat (6);} if (S16.charat (7) >= && S16.charat (7) <= 122) {c16_7 = (char) (S16.charat (7)-+);} else {c16_7 = s16.ch Arat (7);} Sb.append ("=" + C16_6 + c16_7);}}} return sb.tostring ();}}

Package Com.yfli.entity;public class Addressbean {private string truename;private string Mobile;private string Workmobile;private string Telephone;private string Email;public string Gettruename () {return truename;} public void Settruename (String truename) {this.truename = Truename;} Public String Getmobile () {return mobile;} public void Setmobile (String mobile) {this.mobile = mobile;} Public String Getworkmobile () {return workmobile;} public void Setworkmobile (String workmobile) {this.workmobile = Workmobile;} Public String Gettelephone () {return telephone;} public void Settelephone (String telephone) {this.telephone = telephone;} Public String Getemail () {return email;} public void Setemail (String email) {this.email = email;}}


Java parsing vcf format file import and Export

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.