Android逐行读取大文件

我正在开发一个连接到Web服务器并接收响应的应用程序.我将此响应保存在设备内部存储器的txt文件中.现在我需要逐行读取整个文件,因为如果我尝试读取整个文件,它会抛出一个OutofMemoryException.所以现在我正在使用以下代码编写和读取文件:

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

所以任何想法如何读取下一行,因为此代码现在正在做的事情是读取第一行,然后再次尝试读取第一行.

最佳答案 你的bufferedReader实现看起来很好,所以我怀疑你的handleDataFromSync方法会发生什么.从代码中我可以推断出每个responseBody都是一个不确定长度的字符串,但在已知位置具有重复的值模式.潜在的罪魁祸首可能是你无法检查的packetFieldSizes []和dataSize的值.

验证索引记录的值是您期望的值,并且实际上是从handleDataFromSync中获取的.如果是这种情况,那么你的bufferedReader有些问题我没看到,你需要添加一些额外的日志来确定导致该步骤失败的原因.

点赞