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 boolean isMIUI() {
    String propName = "ro.miui.ui.version.name";
    String line;/*from   w  w w  .ja va2  s.  co m*/
    BufferedReader input = null;
    try {
        Process p = Runtime.getRuntime().exec("getprop " + propName);
        input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        return false;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            }
        }
    }
    return line != null && line.length() > 0;
}

From source file:Main.java

public static String getURLContent(String urlStr) throws Exception {
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);//from   w w  w  .  ja  v a  2  s.  c o  m
    connection.connect();
    OutputStream ous = connection.getOutputStream();
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ous));
    bw.write("index.htm");
    bw.flush();
    bw.close();

    printRequestHeaders(connection);
    InputStream ins = connection.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
    StringBuffer sb = new StringBuffer();
    String msg = null;
    while ((msg = br.readLine()) != null) {
        sb.append(msg);
        sb.append("\n"); // Append a new line
    }
    br.close();
    return sb.toString();
}

From source file:Main.java

public static String exec(String cmd) {
    StringBuilder sb = new StringBuilder();
    try {//from w  w w .  jav  a2  s. c o  m
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(cmd);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        if (process.waitFor() != 0) {
            System.err.println("exit value = " + process.exitValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException {
    BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
    StringBuilder s = new StringBuilder();
    String line;/*from w  ww  .ja  v a 2 s .c o  m*/
    while ((line = rd.readLine()) != null) {
        s.append(line);
    }
    rd.close();
    String str = s.toString();
    if (str == null || str.isEmpty()) {
        return null;
    }
    return new JSONObject(str);
}

From source file:net.foxgenesis.helper.SiteReader.java

/**
 * Get a sites HTML and store it by line
 *
 * @param url/*from w  w w .j  a  v  a 2 s. co m*/
 *            - site url
 * @return site's HTML by line
 * @throws IOException
 */
public static String[] getHTMLLines(URL url) throws IOException {
    BufferedReader in = getStream(url);
    ArrayList<String> output = new ArrayList<String>();
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        output.add(inputLine);
    in.close();
    return output.toArray(new String[] {});
}

From source file:Main.java

public static String getSystemProperty(String propName) {
    String line;/*from   w  w  w .j ava2  s. c om*/
    BufferedReader input = null;
    try {
        Process p = Runtime.getRuntime().exec("getprop " + propName);
        input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        Log.e(TAG, "Unable to read sysprop " + propName, ex);
        return null;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                Log.e(TAG, "Exception while closing InputStream", e);
            }
        }
    }
    return line;
}

From source file:com.mobandme.ada.examples.advanced.model.helpers.CountriesLoaderHelper.java

/**
 * This method read a countries.json file from application Assets folder and return a list of Countries.
 * @param pContext/*from w w w . jav  a2  s  .  c o m*/
 * @return List with filled Countries.
 */
public static List<Country> getList(Context pContext) {
    List<Country> returnedValue = new ArrayList<Country>();

    try {
        String data = null;

        if (pContext != null) {

            InputStream input = pContext.getAssets().open("countries.json");
            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 Country(dataParser.getString(index)));
                    }
                }
            }
        }

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

    return returnedValue;
}

From source file:net.chunkyhosting.Roe.CHGManagerLauncher.utils.JSON.java

public static JSONObject readJsonFromFile(File file)
        throws FileNotFoundException, IOException, NullPointerException {

    String json = "NULL";
    BufferedReader reader = new BufferedReader(new FileReader(file.toString()));
    String read = "";
    while ((read = reader.readLine()) != null) {

        json = json + read;//www  . jav  a2  s. c  o m

    }

    reader.close();

    if (!json.equalsIgnoreCase("NULL")) {

        return stringToJson(json);

    }

    throw new NullPointerException();

}

From source file:Main.java

public static String readFromAssetsFile(Context context, String name) throws IOException {
    AssetManager am = context.getAssets();
    BufferedReader reader = new BufferedReader(new InputStreamReader(am.open(name)));
    String line;//from   w  w  w  .j a v a  2 s.c  o  m
    StringBuilder builder = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        builder.append(line);
    }
    reader.close();
    return builder.toString();
}

From source file:Main.java

public static String convertInputStreamReaderToString(InputStreamReader inputStreamReader) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder stringBuilder = new StringBuilder();
    String line;//from w w w . j  a v a2s  .  c  o  m
    while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }
    bufferedReader.close();
    return stringBuilder.toString();
}