Example usage for java.io BufferedReader close

List of usage examples for java.io BufferedReader close

Introduction

In this page you can find the example usage for java.io BufferedReader close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static String responseContent(String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;/*  w  w w.j a  v a 2 s.  c om*/
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}

From source file:Main.java

/**
 *
 * GetAssetsFile allows you to open a file that exists in the Assets directory.
 *
 * @param context//from  w  ww .j  a  v a2 s .c  o  m
 * @param fileName
 * @return the contents of the file.
 */
public static String getAssetsFile(Context context, String fileName) {
    try {
        String file = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));

        // do reading
        String line = "";
        while (line != null) {
            file += line;
            line = reader.readLine();
        }

        reader.close();
        return file;

    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String readSkeleton(AssetManager am, String filename) throws FileNotFoundException, IOException {
    BufferedReader skeletonFile = new BufferedReader(new InputStreamReader(am.open(filename)));
    StringBuilder builder = new StringBuilder();
    String line;//from w  ww  .  ja  v  a2s.co m

    while ((line = skeletonFile.readLine()) != null) {
        builder.append(line);
        builder.append('\n');
    }
    skeletonFile.close();
    return builder.toString();
}

From source file:Main.java

public static String getProp(String name) {
    String line = null;//from  w w  w. j  a  va2s. c  o m
    BufferedReader input = null;
    try {
        Process p = Runtime.getRuntime().exec("getprop " + name);
        input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        Log.e(TAG, "Unable to read prop " + name, ex);
        return null;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return line;
}

From source file:com.googlecode.jsendnsca.MessagePayloadTest.java

private static String getShortHostNameFromOS() throws Exception {
    final Runtime runtime = Runtime.getRuntime();
    final Process process = runtime.exec("hostname");
    final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

    final String expectedHostName = input.readLine();
    input.close();
    assertEquals(0, process.waitFor());/*  w ww  .ja va2  s  .  c o  m*/

    return expectedHostName;
}

From source file:Main.java

/**
 * /*w  w  w .  j  a va  2s  . c  om*/
 * @param aFile
 * @return
 */
public static String getLastLines(String filename, int number) {
    File aFile = new File(filename);
    StringBuilder contents = new StringBuilder();
    LinkedList<String> ll = new LinkedList<String>();

    try {
        BufferedReader input = new BufferedReader(new FileReader(aFile));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                ll.add(line);
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        Log.e(TAG, ex.getMessage());
    }

    if ((ll.size() - number) <= 0) {
        Log.e(TAG, "Requested number of lines exceeds lines of file");
        return "Requested number of lines exceeds lines of file";
    }

    for (int i = (ll.size() - 1); i >= (ll.size() - number); i--) {
        contents.append(ll.get(i - 1));
        contents.append("\n");
    }

    return contents.toString();
}

From source file:test.LocationCrawler.java

private static String ProcessLocationRequest(String QueryItem) {
    try {/* w  w  w  . j a  va 2  s.  c  o m*/
        QueryItem = URLEncoder.encode(QueryItem, "ISO-8859-1");
        String URLStr = String.format(
                "https://maps.googleapis.com/maps/api/geocode/json?" + "address=%s&sensor=false&key=%s",
                QueryItem,
                //"AIzaSyAOkEu7MBxUj4t2pBq1GZ-0Td7cf7bOKTg");
                //"AIzaSyCvzTx408371P7CtoXN8BejAAmBa0NUZbc");
                "AIzaSyAIyQ3XaHrC9TQVMIs65IrwzREtnzqZRKw");
        URL url = new URL(URLStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        if (connection.getResponseCode() == 200) {
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String sb = "";
            String str;

            while ((str = br.readLine()) != null) {
                sb += (str);
            }

            br.close();

            return sb;
        } else {
            return "";
        }
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String stringFromHttpGet(String urlString) {
    try {//from ww w.ja  v a  2s  . c  o  m
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
        return null;
    }
}

From source file:com.mobandme.ada.examples.q.model.helpers.ImportLoaderHelper.java

/**
 * This method read a categories.json file from application Assets folder and return a list of Categories.
 * @param pContext// w  ww . j a v a 2 s  .  c o m
 * @return List with filled Categories.
 */
public static List<Category> getCategoriesList(Context pContext) {
    List<Category> returnedValue = new ArrayList<Category>();

    try {
        String data = null;

        if (pContext != null) {

            InputStream input = pContext.getAssets().open(CATEGORIES_ASSET_NAME);
            if (input != null) {

                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                if (reader != null) {
                    data = reader.readLine();
                    reader.close();
                }

                input.close();
            }

            if (data != null && !data.trim().equals("")) {
                JSONArray dataParser = new JSONArray(data);
                if (dataParser != null && dataParser.length() > 0) {
                    for (int index = 0; index < dataParser.length(); index++) {
                        returnedValue.add(new Category(dataParser.getString(index)));
                    }
                }
            }
        }

    } catch (Exception e) {
        ExceptionsHelper.manage(pContext, e);
    }

    return returnedValue;
}

From source file:com.mobandme.ada.examples.q.model.helpers.ImportLoaderHelper.java

/**
 * This method read a products.json file from application Assets folder and return a list of Products.
 * @param pContext/*from w ww .j  ava 2s .  c o  m*/
 * @return List with filled Products.
 */
public static List<Product> getProductList(Context pContext, Category pCategory) {
    List<Product> returnedValue = new ArrayList<Product>();

    try {
        String data = null;

        if (pContext != null) {

            InputStream input = pContext.getAssets().open(PRODUCTS_ASSET_NAME);
            if (input != null) {

                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                if (reader != null) {
                    data = reader.readLine();
                    reader.close();
                }

                input.close();
            }

            if (data != null && !data.trim().equals("")) {
                JSONArray dataParser = new JSONArray(data);
                if (dataParser != null && dataParser.length() > 0) {
                    for (int index = 0; index < dataParser.length(); index++) {
                        returnedValue.add(new Product(dataParser.getString(index), pCategory));
                    }
                }
            }
        }

    } catch (Exception e) {
        ExceptionsHelper.manage(pContext, e);
    }

    return returnedValue;
}