如何使用java创建vcf文件?

时间:2015-05-11 17:01:02

标签: java vcard vcf

请帮助我。如何在java中创建vcard文件? 它必须包含多个字符串 String name String family name String phone String email

..以及如何将类型设置为homework等属性? 我还没有在互联网上找到任何有用的东西,所以我想转向Stackoverflow社区寻求一些建议。

2 个答案:

答案 0 :(得分:2)

您可以像https://github.com/mangstadt/ez-vcard一样使用Java lib,或者只创建一个符合Vcard的符号String并将其写入文件。有关vcard的信息,请参阅wiki页面http://en.wikipedia.org/wiki/VCard

答案 1 :(得分:1)

使用pur java创建vcf文件。我偶然发现了这个:http://luckyjava.blogspot.de

因为网址可能无效,所以源代码为:

import java.io.*;

public class Vcard
{
 public static void main(String[] args) throws IOException
 {
  File f=new File("contact.vcf");
  FileOutputStream fop=new FileOutputStream(f);

  if(f.exists())
  {
   String str="BEGIN:VCARD\n" + 
     "VERSION:4.0\n" +
     "N:Gump;Forrest;;;\n" +
     "FN:Forrest Gump\n"+
     "ORG:Bubba Gump Shrimp Co.\n"+
     "TITLE:Shrimp Man\n"+
     "TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212\n"+
     "TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212\n"+
     "EMAIL:forrestgump@example.com\n"+
     "REV:20080424T195243Z\n"+
     "END:VCARD";
   fop.write(str.getBytes());
   //Now read the content of the vCard after writing data into it
   BufferedReader br = null;
   String sCurrentLine;
   br = new BufferedReader(new FileReader("contact.vcf"));
   while ((sCurrentLine = br.readLine()) != null)
   {
    System.out.println(sCurrentLine);
   }
   //close the output stream and buffer reader 
   fop.flush();
   fop.close();
   System.out.println("The data has been written");
  } else 
   System.out.println("This file does not exist");
 }
}