From 525fff65e15e88377be3cd80c36f258330546cd7 Mon Sep 17 00:00:00 2001 From: Irena Shaigorodsky Date: Fri, 10 Jul 2015 16:22:10 -0400 Subject: [PATCH] Close file after read --- .../labs/fragmentslab/FeedFragmentData.java | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/Fragments/Lab3c_FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java b/Fragments/Lab3c_FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java index ab32ea70..9f8c620f 100755 --- a/Fragments/Lab3c_FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java +++ b/Fragments/Lab3c_FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java @@ -34,40 +34,54 @@ public FeedFragmentData(Context context) { private void loadFeeds() { for (int id : IDS) { + InputStream inputStream = null; + BufferedReader reader = null; + try { + inputStream = mContext.getResources().openRawResource( + id); + reader = new BufferedReader(new InputStreamReader( + inputStream)); + StringBuffer buffer = new StringBuffer(""); - InputStream inputStream = mContext.getResources().openRawResource( - id); - BufferedReader reader = new BufferedReader(new InputStreamReader( - inputStream)); - StringBuffer buffer = new StringBuffer(""); - - - // Read raw data from resource file - - try { + // Read raw data from resource file + + try { + + String line = ""; + while ((line = reader.readLine()) != null) { - String line = ""; - while ((line = reader.readLine()) != null) { - - buffer.append(line); + buffer.append(line); + } + + } catch (IOException e) { + Log.i(TAG, "IOException"); } - - } catch (IOException e) { - Log.i(TAG, "IOException"); - } - // Convert raw data into a String + // Convert raw data into a String - JSONArray feed = null; - try { - feed = new JSONArray(buffer.toString()); - } catch (JSONException e) { - Log.i(TAG, "JSONException"); + JSONArray feed = null; + try { + feed = new JSONArray(buffer.toString()); + } catch (JSONException e) { + Log.i(TAG, "JSONException"); + } + + mFeeds.put(id, procFeed(feed)); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException ioe) {} + } + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException ioe) {} + } } - mFeeds.put(id, procFeed(feed)); - + } }