2011-10-28 78 views
2

我使用此代碼。如何在android中逐行閱讀?

try{ 
      // Open the file that is the first 
      // command line parameter 
      FileInputStream fstream = new FileInputStream("config.txt"); 
      // Get the object of DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      while ((br.readLine()) != null) { 
       temp1 = br.readLine(); 
       temp2 = br.readLine(); 

      } 

      in.close(); 
    }catch (Exception e){//Catch exception if any 
    Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show(); 
    } 
    Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show(); 

但這是顯示異常並且不更新temp1和temp2。

+1

你想存儲在2級不同的變量在同一行?或者你只是想將最後2行保存在temp1和temp2中? – SERPRO

+0

想要在兩個不同的變量中保存前兩行文件。 – xkrlaix

回答

7

你看到的例外 - 我會強烈建議)來捕捉一個特定的類型,例如IOException和b)記錄或顯示消息或堆棧跟蹤,以及c)至少從DDMS角度檢查LogCat中是否使用Eclipse進行編程 - 可能是由於Android沒有找到您的文件config.txt正試圖打開。通常,對於最簡單的情況(例如您的應用程序),使用openFileInput - see the documentation來打開專用於應用程序的文件以獲取詳細信息。

除了例外情況,您的讀取循環有缺陷:您需要在輸入前初始化一個空字符串,並將其填入while的條件中。

String line = ""; 
while ((line = br.readLine()) != null) { 
    // do something with the line you just read, e.g. 
    temp1 = line; 
    temp2 = line; 
} 

但是,如果您只是想將前兩行保存在不同的變量中,則不需要循環。

String line = ""; 
if ((line = br.readLine()) != null) 
    temp1 = line; 
if ((line = br.readLine()) != null) 
    temp2 = line; 

正如其他人已經指出,調用readLine消耗線,因此,如果您config.txt文件僅包含一行代碼消耗它的while條件,然後temp1temp2得到null分配,因爲沒有更多的文字讀書。如果你想保存你所要做的前兩行

+0

我正在使用此文件 FileInputStream fstream = new openFileInput(「config.txt」); 但它不工作。它無法解決openFileInput的事情。 – xkrlaix

+0

正如你從我的答案中鏈接的文檔中看到的那樣,'openFileInput'是'Context'的一種方法,'Activity'源於此。因此,如果你在一個活動中使用片段,'openFileInput'應該已經可用。否則,您需要將'Context'實例傳遞給您想要讀取文件的方法或類。 –

+0

謝謝。它實際上工作。 – xkrlaix

1
try{ 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream("config.txt"); 
     // Get the object of DataInputStream 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String line = ""; 
     while ((line = br.readLine()) != null) { 
      temp1 = line; 
      temp2 = line; 

     } 

     in.close(); 
}catch (Exception e){//Catch exception if any 
Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show(); 
} 
Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show(); 
1

br.readLine()in while已經佔用一行。

試試這個

LineNumberReader reader = new LineNumberReader(new FileReader("config.txt"))); 
    String line; 
    while ((line = reader.readLine()) != null) { 
     //doProcessLine 
    } 
0

try 
{ 
    // Open the file that is the first 
    // command line parameter 
    FileInputStream fstream = new FileInputStream("config.txt"); 
    // Get the object of DataInputStream 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String line = ""; 
    if((line = br.readLine()) != null) 
     temp1 = line; 
    if((line = br.readLine()) != null) 
     temp2 = line; 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

實際上它顯示異常,因爲它找不到「config.txt」。但如果我使用「FileInputStream fstream = new openFileInput(」config.txt「);」那麼它不承認它。 – xkrlaix

+0

好的。那是另一個問題。無論如何,該代碼是您需要獲取文件的前兩行(當它存在時)。 :) – SERPRO

+0

是的,他們實際存在。我分開看到他們。 – xkrlaix