1

我正在尝试解析 Vcard 文件。这是我的代码。

public void get_vcf_data(String file) throws VCardException, IOException{
         VCardParser parser = new VCardParser();
            VDataBuilder builder = new VDataBuilder();

            //String file = path;

            //read whole file to string
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), "UTF-8"));

            String vcardString = "";
            String line;
            while ((line = reader.readLine()) != null) {
                vcardString += line + "\n";
            }
            reader.close();

            //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;
                String number = null;
                String tel = null;
                for (PropertyNode prop : props) {
                    if ("FN".equals(prop.propName)) {
                        name = prop.propValue;
                        Contact_name.add(name);
                        Log.d("Name", name);
                        //we have the name now
                        break;
                    }
                }
                for (PropertyNode prop : props) {
                    if ("N".equals(prop.propName)) {
                        number = prop.propValue;
                        Contact_number.add(number);
                        Log.d("Name", number);
                        //we have the name now
                        break;
                    }
                }
                for (PropertyNode prop : props) {
                    if(" TEL".equals(prop.propName))
                    {
                        tel = prop.propValue;
                        Contact_tel.add(tel);
                        Log.d("Name", tel);
                    }
                }
                Log.d("Tag", ""+Contact_name.size()+"::"+Contact_number.size()+"::"+Contact_tel.size());

                //similarly for other properties (N, ORG, TEL, etc)
                //...

                System.out.println("Found contact: " + name);
            }
}

但在while循环中遇到问题

while ((line = reader.readLine()) != null) {
                vcardString += line + "\n";
            }

在while循环中不断循环,一旦进入就不会退出循环

4

0 回答 0