2014-09-26 102 views
-3

我在讀取和寫入arraylist到文本文件時遇到問題。具體閱讀。我想要做的是從文本文件中讀取並將其傳送到數組列表中。之後,我會編輯列表並將其寫回文本文件。我認爲我完成了寫作,但沒有閱讀。我試過在這裏閱讀幾個類似的問題,但似乎無法將其注入到我的代碼中。讀取和寫入arraylist到文本文件

讀碼

public void read(List<AddressBook> addToList){ 
    BufferedReader br = null; 
    try { 
     String currentLine= ""; 
     br = new BufferedReader(new FileReader("bank_account.csv"));//file na gusto mo basahin 
     while ((currentLine = br.readLine()) != null) { 
      System.out.println(currentLine); // print per line 

       for (AddressBook read : addToList) { 
        br.read(read.getName() + read.getAddress() + read.getTelNum() + read.getEmailAdd()); 
        addToList.add(read); 
       }   } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (br != null) 
      { 
       br.close(); 
      } 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

}

這裏就是我與寫

public void write(List<AddressBook> addToList) { 
     try { 
      File file = new File("bank_account.csv"); //file 
      // if file doesnt exists, then create it 
      if (!file.exists()) { 
       file.createNewFile(); 
      } 
      //FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
      FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); 
      BufferedWriter bw = new BufferedWriter(fw); 
      for (AddressBook write : addToList) { 
       bw.write(write.getName() + "," + write.getAddress() + "," + write.getTelNum() + "," + write.getEmailAdd()); 
       bw.newLine(); 
      } 
      bw.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+1

那麼你的問題是什麼?你有什麼問題/錯誤? – forgivenson 2014-09-26 16:25:00

+0

與您的問題無關,但您確實應該使用[try-with-resources](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。這將清理很多你的try/catch/finally代碼。 – wolfcastle 2014-09-26 16:28:26

回答

0
while ((currentLine = br.readLine()) != null) { 
     System.out.println(currentLine); // print per line 

      for (AddressBook read : addToList) { 
       br.read(read.getName() + read.getAddress() + read.getTelNum() + read.getEmailAdd()); 
       addToList.add(read); 
      } 
} 

我在那裏敢打賭,你將需要做的是這樣完成的:

  1. 閱讀每一行
  2. 解析它(每一行是一個CSV)
  3. 創建一個新的地址簿對象與所有的信息
  4. 將其添加到收藏

該代碼將看起來像:

while ((currentLine = br.readLine()) != null) { 
     System.out.println(currentLine); // print per line 
     String[] splitted = currentLine.split(","); 
     AddressBook address = new AddressBook(splitted[0], splitted[1], splitted[2], splitted[3]); 

     addToList.add(address); 
} 

當然,有些事情您需要檢查和驗證,但那確實是事實。

0

也許你需要這樣的閱讀方法。

public void read() { 
     List<AddressBook> addToList =new ArrayList<AddressBook>(); 
     BufferedReader br = null; 
     try { 
      String currentLine= ""; 
      br = new BufferedReader(new FileReader("bank_account.csv"));//file na gusto mo basahin 
      while ((currentLine = br.readLine()) != null) { 
       System.out.println(currentLine); // print per line 

//     for (AddressBook read : addToList) { 
         String[] split =currentLine.split(","); 
         AddressBook read = new AddressBook(); 
         read.setName(split[0]); 
         read.setAddress(split[1]); 
         read.setTelNum(split[2]); 
         read.setEmailAdd(split[3]); 
//      br.read(read.getName() + read.getAddress() + read.getTelNum() + read.getEmailAdd()); 
         addToList.add(read); 
//     }   
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null) 
       { 
        br.close(); 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    }