2014-03-04 103 views
-6
 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()); */ 
       } 
+0

有什麼問題你的代碼?有沒有編譯錯誤?例外? – PakkuDon

+0

線程「main」java.lang.ArrayIndexOutOfBoundsException中的異常異常:1在project2.Project2.main – Natalie007

+0

我想讀取文本文件中有兩個名稱的行,用逗號分隔natalie,sophenska brad,holer – Natalie007

回答

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(); 

你可以僅僅刪除這些行。

+0

像魔術一樣工作謝謝 – Natalie007

+0

爲了增加對實際內容的理解,可以在繼續之前添加一個打印輸出,以說明爲什麼跳過該行:System.out.println(「Skipping line:」+ line); – JenEriC

+0

不客氣,娜塔莉) – AnatolyG

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有2類:

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) {} 
       } 
      } 
     } 
    } 
+0

是[email protected] – Natalie007