2011-10-24 119 views
4

我正在處理一個連接到Web服務器並接收響應的應用程序。我將此響應保存在設備的內部存儲器中的txt文件中。現在我需要逐行讀取整個文件,因爲如果我嘗試讀取整個文件,它會拋出一個OutofMemoryException。所以現在我正在寫,用這個代碼讀取文件:Android逐行讀取大文件

httpclient = new DefaultHttpClient(); 
    httppost = new HttpPost("http://www.rpc.probnata.com"); 


    postParameters = new ArrayList<NameValuePair>(); 
    postParameters.add(new BasicNameValuePair("debug_data","1")); 


    httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 

    HttpResponse response = httpclient.execute(httppost); 
    Log.w("Response ","Status line : "+ response.getStatusLine().toString()); 
    buffer = EntityUtils.toByteArray(response.getEntity()); 
    FileOutputStream out = this.openFileOutput("response",this.MODE_PRIVATE); 
    out.write(buffer); 
    out.close(); 

讀文件:

public void parseResponse(){ 
    try { 
     File myDir = new File(getFilesDir().getAbsolutePath()); 

     BufferedReader br = new BufferedReader(new FileReader(myDir + "/response")); 
     String line; 
     while ((line = br.readLine()) != null) { 
      Log.v("","line : "+line); 
      handleDataFromSync(line); 
     } 
     br.close(); 



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

這種方法解析響應。

public void handleDataFromSync(final String responseBody) { 

     for(int index=0;index<responseBody.length();index++){ 
       Log.w("Response ","Response size : "+ responseBody.length()); 
       Log.w("","****************Index is : "+index); 

       int objectIdentificator = 0; 
       objectIdentificator = Integer.parseInt(responseBody.substring(index,index+packetFieldSizes[0])); 
       Log.w("Response ","Object Identificator (LONGINT) : "+ objectIdentificator); 
       index = index+packetFieldSizes[0]; 
       Log.w("","****************Index is (must be 32) : "+index); 

       String type = null; 
       type = responseBody.substring(index,index + packetFieldSizes[1]); 
       Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ type); 
       short pType = Short.parseShort(type); 
       Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ pType); 

       index = index + packetFieldSizes[1]; 
       Log.w("","****************Index is (must be 35) : "+index); 

       String operation=null; 
       operation = responseBody.substring(index,index + packetFieldSizes[2]); 
       short operationType = Short.parseShort(operation); 
       Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operation); 
       Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType); 
       index = index + packetFieldSizes[2]; 
       Log.w("","****************Index is (must be 38) : "+index); 

       String objectId=null; 
       objectId = responseBody.substring(index, index + packetFieldSizes[3]); 
       Log.w("Response ","UID (CHAR, length 32) : "+ objectId); 
       index = index + packetFieldSizes[3]; 
       Log.w("","****************Index is (must be 70) : "+index); 

       int id=0; 
       id = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[4])); 
       Log.w("Response ","ID (LONGINT) : "+ responseBody.substring(index, index + packetFieldSizes[4])); 
       Log.w("Response ","ID (LONGINT) : "+ id); 
       index = index + packetFieldSizes[4]; 
       Log.w("","****************Index is (must be 102) : "+index); 

       String size=null; 
       size = responseBody.substring(index,index + packetFieldSizes[5]); 
       int dataSize = Integer.parseInt(size); 
       Log.w("Response ","Data Size (LONGINT) : "+ dataSize); 
       index = index + packetFieldSizes[5]; 
       Log.w("","****************Index is (must be 134) : "+index); 

       String hash=null; 
       hash = responseBody.substring(index,index + packetFieldSizes[6]); 
       Log.w("Response ","Data Hash (CHAR, length 32 : "+ hash); 
       index = index + packetFieldSizes[6]; 
       Log.w("","****************Index is (must be 166) : "+index); 

       String dType=null; 
       dType = responseBody.substring(index,index + packetFieldSizes[7]); 
       Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dType); 
       short dataType = Short.parseShort(dType); 
       Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dataType); 
       index = index + packetFieldSizes[7]; 
       Log.w("","****************Index is (must be 169) : "+index); 

       String data=null; 
       data = responseBody.substring(index, index + dataSize); 
       Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data); 

       index = (index + dataSize)-1; 
       Log.w("","****************Index is must be : "+index); 
       byte[] first = Base64.decode(data); 
       String string = new String(first, "UTF-8"); 
       Log.w("Response ","BASE 64 : "+ string); 
     } 
} 

所以任何想法如何讀取下一行,因爲此代碼現在做的事情是閱讀的第一行後,它的tryint再次讀取第一行。

回答

2

你的bufferedReader實現看起來不錯,所以我懷疑你的handleDataFromSync方法會有什麼問題。從代碼我會推斷每個responseBody是一個不確定長度的字符串,但在已知位置重複值的模式。潛在的罪魁禍首可能是我們無法檢查的packetFieldSizes []和dataSize的值。

驗證索引記錄的值是您期望它們是什麼,並且您實際上是將它從handleDataFromSync中解析出來。如果是這種情況,那麼您的bufferedReader出現了一些我沒有看到的錯誤,您需要添加一些額外的日誌記錄來確定導致該步驟失敗的原因。

+0

其實我發現問題在於讀取文件。如果我的迴應小於30K,則會在響應結束時添加一些字符以達到30K,並且導致異常。感謝你的回答!我將其標記爲已接受,因爲您是唯一回答我的問題的人:)謝謝! –

+0

很高興你能解決它,Bombastic! – Carth