`
长安长宁
  • 浏览: 27312 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

cardme 解析塞班系统通讯录Symbain vcf

阅读更多

public class vcfTest {
	private VCardEngine vcardEngine = null;// vcf engine,used to parse vcf

	public vcfTest() {
		vcardEngine = new VCardEngine();// initialization engine
		vcardEngine.setCompatibilityMode(CompatibilityMode.RFC2426);// rfc mode
	}

	public void readVcfDir(String fname) {
		File dir = new File(fname);// read vcf dir
		FilenameFilter ff = new FilenameFilter() {
			public boolean accept(File d, String fn) {
				if (fn.endsWith(".vcf"))
					return true;
				return false;
			}
		};// filer bad vcf out
		File[] vcfs = dir.listFiles(ff);// get all vcfs from dir
		for (int i = 0; i < vcfs.length; i++) {
			readVcf(vcfs[i]);
		}

	}

	public void readVcf(File vcf) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(vcf), "UTF-8"));
			String temp = br.readLine();
			String s = "";
			int M, Y, D;
			while (temp != null) {// read vcf into string s
				if (temp.startsWith(" ") || temp.startsWith("="))
					s = s + temp.trim();
				else
					s = s + "\n" + temp.trim();
				if (temp.trim().equals("END:VCARD")) {
					break;
				}
				temp = br.readLine();
			}

			br.close();

			VCard vcard = vcardEngine.parse(s);// parse vcf
			String name = null;

			if (vcard.getName() != null) {// get name
				name = vcard.getName().getFamilyName().trim();
				if (vcard.getName().hasGivenName())
					name = name + " " + vcard.getName().getGivenName().trim();
			}

			// if (name == null || name.equals(""))
			// name = vcf.getName().substring(0, vcf.getName().indexOf("."));

			if (vcard.hasExtendedTypes()) {// get types starts with "X-"
				Iterator<ExtendedFeature> extFeatures = vcard
						.getExtendedTypes();
				while (extFeatures.hasNext()) {
					ExtendedFeature feature = extFeatures.next();
					String featureName = feature.getExtensionName().trim();
					String data = feature.getExtensionData().trim();
					if (featureName.equalsIgnoreCase("X-BDAY")) {// birthday for
																	// example,etc.
						String[] ymd = data.split("-");
						if (ymd.length >= 3) {
							Y = Integer.parseInt(ymd[0]);// year
							M = Integer.parseInt(ymd[1]);// month
							D = Integer.parseInt(ymd[2]);// day
						}
					}

				}
			}
			System.out.println(name);// print names
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}

	}

	public static void main(String[] args) {
		new vcfTest().readVcfDir("C:\\Users\\Desktop\\1");// main function starts here
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics