Marek Hladnis
Marek Hladnis

Reputation: 174

How to create vcf file using java?

Could someone help me, please. How to create vcard file in java? It must contain several strings String name String family name String phone String email

..and how to set types to these attributes such as home,work,etc? I haven't found anything useful over the internet, so I would like to turn to Stackoverflow community for some advices.

Upvotes: 1

Views: 12349

Answers (2)

Reporter
Reporter

Reputation: 3948

Creating a vcf file with pur java. I have found this by accident: http://luckyjava.blogspot.de

Because an url can going invalid, here the source code:

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:[email protected]\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");
 }
}

Upvotes: 1

mszalbach
mszalbach

Reputation: 11440

You can use a Java lib for this like https://github.com/mangstadt/ez-vcard or you just create a vcard conform String and write it into a File. For information how a vcard looks like see the wiki page http://en.wikipedia.org/wiki/VCard.

Upvotes: 2

Related Questions