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 readFileByLines(File file) {
    String string = ""; //$NON-NLS-1$
    BufferedReader reader = null;
    try {/* w  w  w.  j av a  2s .  c om*/
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); //$NON-NLS-1$
        String tempString = null;
        while ((tempString = reader.readLine()) != null) {
            string += tempString;
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e1) {
            }
        }
    }
    return string;
}

From source file:Main.java

private static String readFirstLine(Context context, String fileName) {
    BufferedReader reader = null;
    try {//w w  w  .ja v a 2 s. com
        reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));

        return reader.readLine();
    } catch (IOException e) {
        return "";
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:de.mas.telegramircbot.utils.images.ImgurUploader.java

public static String uploadImageAndGetLink(String clientID, byte[] image) throws IOException {
    URL url;/*from   w w  w .  j  av  a2 s.  com*/
    url = new URL(Settings.IMGUR_API_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String dataImage = Base64.getEncoder().encodeToString(image);
    String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ImgurResponse.class, new ImgurResponseDeserializer());
    Gson gson = gsonBuilder.create();

    // The JSON data
    try {
        ImgurResponse response = gson.fromJson(stb.toString(), ImgurResponse.class);
        return response.getLink();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return stb.toString();
}

From source file:Main.java

public static String readInputStream(InputStreamReader in) {
    StringBuilder sb = new StringBuilder();
    try {/*from   w  w  w . java2s .  co  m*/
        BufferedReader reader = new BufferedReader(in);
        char[] temp = new char[1024];
        int length;
        while ((length = reader.read(temp)) != -1) {
            if (Thread.currentThread().isInterrupted())
                break;
            sb.append(temp, 0, length);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
        }
    }
    return (sb.toString());
}

From source file:Main.java

public static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;/*from w  w  w  . j a  va2  s.co  m*/
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static String textToString(Reader xquery) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(xquery);
    String NL = System.getProperty("line.separator");
    String line = null;/*from  www  .ja  va  2  s. c  o  m*/
    try {
        while ((line = br.readLine()) != null) {
            sb.append(line).append(NL);
        }
        sb.deleteCharAt(sb.length() - 1);
    } finally {
        br.close();
    }
    return sb.toString();
}

From source file:com.github.piasy.biv.example.App.java

static String getSystemProperty(String propName) {
    String line;/*from  w ww.jav  a 2s .c o  m*/
    BufferedReader input = null;
    try {
        Process p = Runtime.getRuntime().exec("getprop " + propName);
        input = new BufferedReader(new InputStreamReader(p.getInputStream(), "UTF-8"), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to read sysprop " + propName, ex);
        return null;
    } finally {
        IOUtils.closeQuietly(input);
    }
    return line;
}

From source file:mxnet.ImageClassification.java

/**
 * Helper class to print the maximum prediction result
 * @param probabilities The float array of probability
 * @param modelPathPrefix model Path needs to load the synset.txt
 *///  w w w. j  a  va 2 s  .  co m
private static String printMaximumClass(float[] probabilities, String modelPathPrefix) throws IOException {
    String synsetFilePath = modelPathPrefix.substring(0, 1 + modelPathPrefix.lastIndexOf(File.separator))
            + "/synset.txt";
    BufferedReader reader = new BufferedReader(new FileReader(synsetFilePath));
    ArrayList<String> list = new ArrayList<>();
    String line = reader.readLine();

    while (line != null) {
        list.add(line);
        line = reader.readLine();
    }
    reader.close();

    int maxIdx = 0;
    for (int i = 1; i < probabilities.length; i++) {
        if (probabilities[i] > probabilities[maxIdx]) {
            maxIdx = i;
        }
    }

    return "Probability : " + probabilities[maxIdx] + " Class : " + list.get(maxIdx);
}

From source file:Main.java

public static List<String> readFile(Reader simpleReader) throws IOException {
    BufferedReader reader = new BufferedReader(simpleReader);
    try {/* w  w  w  . j a v  a  2 s .c o  m*/
        List<String> res = new ArrayList();
        String line;
        while ((line = reader.readLine()) != null) {
            res.add(line);
        }
        return res;
    } finally {
        reader.close();
    }
}

From source file:com.junoyoon.BullsUtil.java

/**
 * Load Resource//from  w w w . j a  va  2  s. c  o m
 * 
 * @param resourceLocation
 * @return
 */
public static String loadResourceContent(String resourceLocation) {
    StringBuilder result = new StringBuilder(1024);
    try {
        InputStream is = BullsUtil.class.getClassLoader().getResourceAsStream(resourceLocation);
        InputStreamReader inputStream = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(inputStream);
        String buffer;
        while ((buffer = br.readLine()) != null) {
            result.append(buffer).append("\n");
        }
        br.close();
    } catch (Exception e) {
        BullsHtml.printErrorAndExit(e);
    }
    return result.toString();
}