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:com.caorongjin.pingguo.Pingguo.java

private static void _cleanup(BufferedReader br) {
    if (br != null) {
        try {//from  www.j  av a 2s  .  c  om
            br.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static String readTextFile(File f) throws IOException {
    BufferedReader br = null;
    String json = "";
    try {/*  w  w w .  java 2  s  .c o m*/

        String sCurrentLine;
        br = new BufferedReader(new FileReader(f));

        while ((sCurrentLine = br.readLine()) != null) {
            json += sCurrentLine;
        }

    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
        }
    }
    return json;
}

From source file:Main.java

static public String download_json(String url_addr) {
    StringBuilder result = new StringBuilder();
    try {/*www  . j ava  2s  .co  m*/
        URL url = new URL(url_addr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if (conn != null) {
            conn.setConnectTimeout(10000);
            conn.setUseCaches(false);
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                for (;;) {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    result.append(line + '\n');
                }
                br.close();
            } else {
                conn.disconnect();
                return null;
            }
            conn.disconnect();
        }
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        return null;
    }
    return result.toString();
}

From source file:be.benvd.mvforandroid.data.MVDataHelper.java

/**
 * Returns the GET response of the given url.
 * //from w w w.  ja v a2 s  . c  o m
 * @throws IOException
 * @return The response of the given URL. If no response was found, null is
 *         returned.
 */
public static String getResponse(String username, String password, String url) throws IOException {
    /*      DefaultHttpClient httpclient = new DefaultHttpClient();
          httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username + ":" + password));
          HttpGet httpget = new HttpGet(url);
          HttpResponse response = httpclient.execute(httpget);
    */
    DefaultHttpClient httpclient = new DefaultHttpClient();
    Credentials creds = new UsernamePasswordCredentials(username, password);
    httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), creds);

    String auth = android.util.Base64.encodeToString((username + ":" + password).getBytes("UTF-8"),
            android.util.Base64.NO_WRAP);

    HttpGet httpget = new HttpGet(url);
    httpget.addHeader("Authorization", "Basic " + auth);
    HttpResponse response = httpclient.execute(httpget);

    if (response.getEntity() != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        Log.v(MVDataHelper.class.getSimpleName(), "Response:" + sb.toString());
        return sb.toString();
    }

    return null;
}

From source file:com.moongene.android.HttpService.java

public static String streamToString(final InputStream stream) throws IOException {
    BufferedReader readBuffer = new BufferedReader(new InputStreamReader(stream));
    StringBuilder sb = new StringBuilder();
    //Read the buffer now and create a string
    String line = null;/*from   www  .  j ava 2 s . co  m*/
    while ((line = readBuffer.readLine()) != null) {
        sb.append(line + "\n");
    }
    readBuffer.close();
    return sb.toString();
}

From source file:com.richtodd.android.repository.JSONUtility.java

public static JSONObject loadJSONObject(InputStream in) throws RepositoryException {
    try {// www  .j  a  v  a 2 s  .c  om
        StringBuilder sb = new StringBuilder();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } finally {
            reader.close();
        }

        String jsonString = sb.toString();

        JSONObject jsonObject = (JSONObject) new JSONTokener(jsonString).nextValue();

        return jsonObject;
    } catch (FileNotFoundException ex) {
        throw new RepositoryException(ex);
    } catch (IOException ex) {
        throw new RepositoryException(ex);
    } catch (JSONException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:Main.java

public static String getFileAsString(File file) {
    try {/*from w  w  w  .ja  v a2 s.  co m*/
        FileInputStream fin = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            if (sb.length() > 0) {
                sb.append("\n");
            }
            sb.append(line);
        }
        reader.close();
        fin.close();
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.velonuboso.made.core.rat.RatNameHelper.java

public static String getText(URL url) throws Exception {
    URLConnection connection = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;// www  .  j  a  v a2s.  c o m

    while ((inputLine = in.readLine()) != null)
        response.append(inputLine + "\n");

    in.close();

    return response.toString();
}

From source file:Main.java

public static String streamToString(InputStream stream, String encoding, boolean keepLineBreaks) {
    try {//  w  ww  . j a  v  a  2  s .  c  o  m
        BufferedReader rd = new BufferedReader(new InputStreamReader(stream, encoding), 65535);
        StringBuilder sb = new StringBuilder();
        String line;

        while ((line = rd.readLine()) != null) {
            sb.append(line);
            if (keepLineBreaks) {
                sb.append("\n");
            }
        }

        rd.close();
        return sb.toString();
    } catch (Throwable err) {
        throw new RuntimeException(err.getMessage(), err);
    }
}

From source file:Main.java

public static String readTextFromURL(String urlString) throws IOException {

    HttpURLConnection urlConnection = null;

    URL url = new URL(urlString);

    urlConnection = (HttpURLConnection) url.openConnection();

    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000 /* milliseconds */);
    urlConnection.setConnectTimeout(15000 /* milliseconds */);

    urlConnection.setDoOutput(true);/*w  ww .  j  a va2s . co  m*/

    urlConnection.connect();

    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

    char[] buffer = new char[1024];

    String jsonString = new String();

    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }
    br.close();

    return sb.toString();
}