如何阅读下一行?

时间:2014-03-04 13:18:10

标签: java arraylist

        package project2;
        import java.io.BufferedReader; 
         import java.io.FileReader; 
         import java.io.IOException;
         import java.util.ArrayList;
        public class Project2 {

        public static void main(String[] args) {

            String FirstName = "";
            String LastName = "";
            ArrayList aList = new ArrayList();

            try {
           BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\vanchi\\Desktop\\test.txt"));


                if(!br.ready())
                    throw new IOException();

                while ((line = br.readLine())!=null){
                    aList.add(line);
                    //System.out.println(line);
                    String tmp[] = line.split(",");
                    FirstName =  tmp[0];
                    LastName = tmp[1];


                    System.out.println(FirstName + "\t" + LastName);
                }
                br.close();

             }catch (IOException e){
                    e.printStackTrace();

           }
           /* int sz = aList.size();

            for(int i=0; i<sz; i++){

                System.out.println(aList.get(i).toString()); */
              }

2 个答案:

答案 0 :(得分:0)

确保文件的每一行确实至少有一个','。并使您的代码更健壮:

      while ((line = br.readLine())!=null){
           aList.add(line);
           String tmp[] = line.split(",");

           if (tmp.length < 2) {
               continue;
           }

           FirstName =  tmp[0];
           LastName = tmp[1];


           System.out.println(FirstName + "\t" + LastName);
       }

另外,我相信你不需要:

    if(!br.ready())
           throw new IOException();

您可以删除这些行。

答案 1 :(得分:0)

所以,你在这里:)

V1:         包natalie.person.v1;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Set;

    public class RunMe {

        public static void main(String[] args) {

            Set<String> allPersons = new HashSet<>(); // or use java.util.TreeSet() to have the list sorted

            File dataFolder = new File("/home/agudkov/projects/rest/Natalie/data"); // change the path, pls

            for (File file : dataFolder.listFiles()) {            
                if (!file.getName().endsWith(".txt")) { // check the file has .txt ext
                    continue;
                }

                addPersonsFromFile(file, allPersons);
            }

            for (String person : allPersons) {
                System.out.println(person);
            }
        }

        private static void addPersonsFromFile(File dataFile, Set<String> toSet) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(dataFile));

                String line;

                while ((line = br.readLine()) != null) {
                    String tmp[] = line.split(",");

                    if (tmp.length < 2) {
                        continue;
                    }

                    String firstName = tmp[0];
                    String lastName = tmp[1];

                    toSet.add(firstName + "\t" + lastName);
                }            

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {}
                }
            }
        }
    }

V2有两个类:

    package natalie.person.v2;

    import java.util.Objects;

    public class Person implements Comparable<Person> {

        public static Person parse(String line) {
            String[] tmp = line.split(",");
            if (tmp.length >= 2) {
                return new Person(tmp[0], tmp[1]);
            }
            return null;
        }

        private final String firstName;
        private final String lastName;

        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 79 * hash + Objects.hashCode(this.firstName);
            hash = 79 * hash + Objects.hashCode(this.lastName);
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Person other = (Person) obj;
            if (!Objects.equals(this.firstName, other.firstName)) {
                return false;
            }
            if (!Objects.equals(this.lastName, other.lastName)) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Person o) {
            int result = 0;
            if (firstName != null) {
                result = firstName.compareTo(o.firstName);
                if (result == 0 && lastName != null) {
                    result = lastName.compareTo(o.lastName);
                }
            }
            return result;
        }        

        @Override
        public String toString() {
            return firstName + '\t' + lastName;
        }        
    }




    package natalie.person.v2;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Set;

    public class RunMe {
        public static void main(String[] args) {

            Set<Person> allPersons = new HashSet<>(); // or use java.util.TreeSet() to have the list sorted

            File dataFolder = new File("/home/agudkov/projects/rest/Natalie/data"); // change the path, pls

            for (File file : dataFolder.listFiles()) {            
                if (!file.getName().endsWith(".txt")) { // check the file has .txt ext
                    continue;
                }

                addPersonsFromFile(file, allPersons);
            }

            for (Person person : allPersons) {
                System.out.println(person);
            }
        }

        private static void addPersonsFromFile(File dataFile, Set<Person> toSet) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(dataFile));

                String line;

                while ((line = br.readLine()) != null) {
                    Person person = Person.parse(line);

                    if (person == null) {
                        continue;
                    }

                    toSet.add(person);
                }            

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {}
                }
            }
        }
    }