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

/**
 * Return the text of the file with the given URL. E.g. if
 * http://test.be/text.txt is given the contents of text.txt is returned.
 * //from   www  .  ja v  a  2  s.c  om
 * @param url
 *            The URL.
 * @return The contents of the file.
 */
public static String readTextFromUrl(URL url) {
    StringBuffer fubber = new StringBuffer();
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            fubber.append(inputLine).append("\n");
        }
        in.close();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
    return fubber.toString();
}

From source file:Main.java

private static String getData(String target, String method, String token) {
    try {/* w ww  . j  a v a 2 s  .  c o  m*/
        URL url = new URL(target);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        if (!TextUtils.isEmpty(token))
            conn.setRequestProperty("Authorization", token);

        conn.setRequestMethod(method);
        conn.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line, data = "";
        while ((line = in.readLine()) != null)
            data += line;
        in.close();

        return data;
    } catch (IOException ignored) {
        ignored.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;/* w w  w . j a v a2  s  .  c o  m*/
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

From source file:com.jjtree.utilities.JConverter.java

public static JSONObject convert(HttpServletRequest request) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = request.getReader();
    try {/*from   w w  w .j  a  v  a 2  s. c o m*/
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } finally {
        reader.close();
    }

    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(sb.toString());
    } catch (JSONException ex) {
        Logger.getLogger(JConverter.class.getName()).log(Level.SEVERE, null, ex);
    }

    return jsonObject;
}

From source file:Main.java

public static void loadSystemClassPath() {
    try {//from  w w  w . j a  va 2s .  co m
        File file = new File("../classpath.txt");
        if (!file.exists())
            return;
        BufferedReader bf = new BufferedReader(new FileReader("../classpath.txt"));
        syspath = bf.readLine();
        System.out.println(syspath);
        File pfile = new File(syspath);
        if (!pfile.exists()) {
            bf.close();
            file.delete();
            return;
        }

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

From source file:Main.java

public static String getSystemProperty(String propName) {
    String line = "";
    BufferedReader input = null;
    try {/* ww  w  .  j av  a 2s  .c o m*/
        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);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                //      Log.e(TAG, "Exception while closing InputStream", e);
            }
        }
    }
    return line;
}

From source file:Main.java

/**
 * read the inputstream to String/*ww  w .java2 s  . co  m*/
 * @param inputStream
 * @return
 * @throws IOException
 */
public static String loadStream(InputStream inputStream) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder sb = new StringBuilder();

    String tmpLine;
    while ((tmpLine = br.readLine()) != null) {
        sb.append(tmpLine);
    }
    br.close();
    return sb.toString();
}

From source file:MD5.java

public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {//w  w  w.  j av  a2 s . c o  m
        Process p = Runtime.getRuntime().exec("su");
        OutputStream os = p.getOutputStream();
        os.write(("md5sum " + recoveryFilename).getBytes());
        os.flush();
        os.close();
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str = br.readLine();
        MD5string = str.split("  ")[0].trim();
        is.close();
        br.close();
        p.destroy();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
    System.out.println(MD5string);
    return MD5string;
}

From source file:com.thoughtworks.go.utils.CommandUtils.java

private static StringBuilder dump(BufferedReader reader, StringBuilder builder) throws IOException {
    String line;// w  w w  .j  a  v  a 2s. com
    while ((line = reader.readLine()) != null) {
        builder.append(line + "\n");
    }
    reader.close();
    return builder;
}

From source file:Main.java

public static String requestPage(String urlPath) throws Exception {
    URL url = new URL(urlPath);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    int responseCode = connection.getResponseCode();
    if (responseCode != 200) {
        throw new Exception("Error loading page: " + responseCode + connection.getResponseMessage());
    }// w  w w  . j av  a  2 s.co m

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    String response = "";

    while ((inputLine = in.readLine()) != null) {
        response += inputLine;
    }

    in.close();
    return response;
}