2012-07-07 134 views
0

我想要完成的是逐行讀取文件並將每行存儲到ArrayList中。這應該是一個這麼簡單的任務,但我一直遇到很多問題。起初,當它被保存迴文件時,它正在重複這些行。似乎經常發生的另一個錯誤是,它跳過了嘗試,但沒有發現異常?我嘗試了幾種技術,但沒有運氣。如果您有任何建議或無論如何可以提供幫助,將不勝感激。謝謝將文件內容添加到ArrayList; Android

當前代碼:

try{ 
    // command line parameter 
    FileInputStream fstream = new FileInputStream(file); 
    // Get the object of DataInputStream 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 

    while ((strLine = br.readLine()) != null) { 
     fileList.add(strLine); 
    } 
    //Close the input stream 
    in.close(); 
} catch (Exception e){//Catch exception if any 
    Toast.makeText(this, "Could Not Open File", Toast.LENGTH_SHORT).show(); 
} 
fileList.add(theContent); 

//now to save back to the file 
try { 
    FileWriter writer = new FileWriter(file); 
    for(String str: fileList) { 
     writer.write(str); 
     writer.write("\r\n"); 
    } 
    writer.close(); 
} catch (java.io.IOException error) { 
    //do something if an IOException occurs. 
    Toast.makeText(this, "Cannot Save Back To A File", Toast.LENGTH_LONG).show(); 
} 

回答

2

有一個很簡單的替代你與Scanner類做什麼:以後

Scanner s = new Scanner(new File("filepath")); 
ArrayList<String> list = new ArrayList<String>(); 
while (s.hasNext()){ 
    list.add(s.next()); 
} 
s.close(); 
+0

這似乎適用於將文件保存到數組列表中。但是,在嘗試保存文件時,它會遇到異常。所以,第二次嘗試塊必須有錯誤。另外,我希望能夠用另一個文件做同樣的事情。但是,當我嘗試使用不同的掃描器變量執行相同的代碼時,它強制關閉。 – chRyNaN 2012-07-07 21:14:05

+0

你是從多個線程同時做到這一點?什麼是拋出的異常? – 2012-07-07 23:41:31

+0

@ user1478764,您可能得到了路徑/文件無效/存在的常見錯誤。檢查路徑/文件。 – 2012-07-08 04:25:18

0

爲什麼你有fileList.add(theContent)試着抓?我不明白這是什麼意思。 刪除該行,看看它是否有幫助。

例子,我剛剛測試此代碼我的本地機器上(不是機器人,但應該是相同的)

import java.io.*; 
import java.util.ArrayList; 
class FileRead 
{ 
public static void main(String args[]) 
    { 
    ArrayList<String> fileList = new ArrayList<String>(); 
    final String file = "textfile.txt"; 
    final String outFile = "textFile1.txt"; 
    try{ 
     FileInputStream fstream = new FileInputStream(file); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 

     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     fileList.add(strLine); 
     } 
     //Close the input stream 
     in.close(); 
    } catch (Exception e){//Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
    } 

    try { 
     FileWriter writer = new FileWriter(outFile); 
     for(String str: fileList) { 
      writer.write(str); 
      writer.write("\r\n"); 
     } 
     writer.close(); 
    } catch (java.io.IOException error) { 
     System.err.println("Error: " + error.getMessage()); 
    } 
    } 
} 

後,我跑這2個文件沒有區別。所以我的猜測是這條線可能與它有關。

+0

它應該是一個評論,因爲你沒有真正回答這個問題。請解決這個問題。 – Egor 2012-07-07 20:38:08

+0

'fileList.add(theContent);'的原因是因爲在將文件的所有內容保存到數組列表後,我想向數組列表中添加另一個值,然後將其重新保存迴文件。 – chRyNaN 2012-07-07 21:10:28