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:pushandroid.POST2GCM.java

public static void post(Content content) {

    try {/*  ww  w.  ja  v a2s.  c  om*/

        // 1. URL
        URL url = new URL(URL);

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);

        conn.setDoOutput(true);

        // 5. Add JSON data into POST request body
        //`5.1 Use Jackson object mapper to convert Contnet object into JSON
        ObjectMapper mapper = new ObjectMapper();

        // 5.2 Get connection output stream
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        // 5.3 Copy Content "JSON" into
        mapper.writeValue(wr, content);

        // 5.4 Send the request
        wr.flush();

        // 5.5 close
        wr.close();

        // 6. Get the response
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

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

        // 7. Print result
        System.out.println(response.toString());

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

From source file:Zip.java

/**
 * Reads a Zip file, iterating through each entry and dumping the contents
 * to the console./*from  w  w w. j a va2s . c om*/
 */
public static void readZipFile(String fileName) {
    ZipFile zipFile = null;
    try {
        // ZipFile offers an Enumeration of all the files in the Zip file
        zipFile = new ZipFile(fileName);
        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            System.out.println(zipEntry.getName() + " contains:");
            // use BufferedReader to get one line at a time
            BufferedReader zipReader = new BufferedReader(
                    new InputStreamReader(zipFile.getInputStream(zipEntry)));
            while (zipReader.ready()) {
                System.out.println(zipReader.readLine());
            }
            zipReader.close();
        }
    } catch (IOException ioe) {
        System.out.println("An IOException occurred: " + ioe.getMessage());
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:index.IncrementIndex.java

public static String getStoreId(String path) {
    String storeId = "";
    try {//from   w w  w . j  a  v a2s.c  o  m
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        storeId = br.readLine();
        if (storeId == null || storeId == "")
            storeId = "0";
        br.close();
        fr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return storeId;
}

From source file:de.zib.gndms.gndms.security.GridMapUserDetailsService.java

public static boolean searchInGridMapfile(final String fileName, final String dn) throws IOException {

    BufferedReader reader = new BufferedReader(new FileReader(fileName));

    try {/*from   w  w  w .  j  a v  a  2  s.c o  m*/
        String line;
        while ((line = reader.readLine()) != null) {

            if (line.startsWith("\"" + dn + "\""))
                return true;

        }
    } finally {
        reader.close();
    }

    return false;
}

From source file:asciidoc.maven.plugin.tools.FileHelper.java

public static String readFile(File f) {
    StringBuilder contents = new StringBuilder();
    try {//from ww w .  jav a2  s.co  m
        BufferedReader input = new BufferedReader(new FileReader(f));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return contents.toString();
}

From source file:com.abid_mujtaba.bitcoin.tracker.services.FetchPriceService.java

private static String HttpResponseToString(HttpResponse response) throws NetworkException {
    try {//from  w w w.j  a  v a 2  s  .  co  m
        InputStream is = response.getEntity().getContent();

        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        String line;
        StringBuilder sb = new StringBuilder();

        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        br.close();

        return sb.toString();
    } catch (UnsupportedEncodingException e) {
        throw new NetworkException("Error converting HttpResponse to String.", e);
    } catch (IOException e) {
        throw new NetworkException("Error converting HttpResponse to String.", e);
    }
}

From source file:com.renlg.util.NetAssist.java

public static String delegateGet(String url) {

    StringBuffer response = new StringBuffer();
    HttpClient client = new HttpClient();
    HttpMethod method = null;/*from ww w .jav  a 2  s .  c  o  m*/
    try {
        method = new GetMethod(url);
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), "utf8"));
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
        }

    } catch (Exception e) {
        log.error("HTTP Get" + url + "??", e);
    } finally {
        if (method != null)
            method.releaseConnection();
    }
    return response.toString();
}

From source file:Main.java

/**
 * Issue a POST request to the server.//  w  ww  .j  a  v a 2  s .co m
 *
 * @param endpoint POST address.
 * @param params request parameters.
 * @return response
 * @throws IOException propagated from POST.
 */
private static String executePost(String endpoint, Map<String, String> params) throws IOException {
    URL url;
    StringBuffer response = new StringBuffer();
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    //Log.v(TAG, "Posting '" + body + "' to " + url);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();

        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
            throw new IOException("Post failed with error code " + status);

        } else {
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    return response.toString();
}

From source file:ilearnrw.utils.ServerHelperClass.java

public static String sendGet(String link) throws Exception {
    String url = baseUrl + link;/* ww w. j  a va 2 s. com*/
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("GET");
    con.setRequestProperty("Authorization", userNamePasswordBase64("api", "api"));
    //con.setRequestProperty("User-Agent", USER_AGENT);

    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();
}

From source file:Main.java

public static String readFromInternalStorage(Context context, String fileName) {

    File file = new File(context.getFilesDir(), fileName);

    try {//from ww  w . j a v a 2  s .  c  o m
        FileInputStream fis = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        String line;

        StringBuilder stringBuilder = new StringBuilder();
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
        }
        String json = stringBuilder.toString();

        br.close();
        in.close();
        fis.close();

        return json;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }

    return null;
}