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 char[] getChars(File file) throws IOException {
    String str;//from  ww  w  .  j av a2s  . c o m

    FileReader reader = new FileReader(file);
    BufferedReader buffer = new BufferedReader(reader);
    String line;
    StringBuilder result = new StringBuilder();
    while ((line = buffer.readLine()) != null) {
        result.append(line).append("\n");
    }
    str = result.toString();
    buffer.close();
    reader.close();

    return str.toCharArray();
}

From source file:Main.java

public static String loadFileAsString(String filePath) throws java.io.IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);//from  ww  w.  ja va 2  s  .com
    }
    reader.close();
    return fileData.toString();
}

From source file:Main.java

public static String readTextFromUri(Context context, Uri uri) throws IOException {
    InputStream inputStream = context.getContentResolver().openInputStream(uri);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder stringBuilder = new StringBuilder();
    String line;/*from w w w  .  ja v a2s  .  co  m*/
    while ((line = reader.readLine()) != null) {
        stringBuilder.append(line);
    }
    inputStream.close();
    reader.close();
    return stringBuilder.toString();
}

From source file:com.htmlhifive.pitalium.it.assertion.exclude.CompareWithExcludesCheckResultTest.java

private static String readExpectedId() throws IOException {
    File file = new File(PtlTestConfig.getInstance().getPersisterConfig().getFile().getResultDirectory()
            + File.separator + TEST_CLASS_NAME + ".json");
    BufferedReader br = new BufferedReader(new FileReader(file));
    String str = br.readLine();//from w w  w .ja v a2  s  . c o m
    br.close();
    return str;
}

From source file:Main.java

public static String getStringFromStream(InputStream stream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream, "UTF8"));
    StringBuilder stringBuilder = new StringBuilder();

    String line;//from w  w w.j ava2 s.  c om
    while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }

    bufferedReader.close();
    return stringBuilder.toString();
}

From source file:Main.java

private static String readXmlFromURL(String theUrl) {
    StringBuilder content = new StringBuilder();
    try {//from  w  w w. j av a2  s .c o m
        URL url = new URL(theUrl);
        URLConnection urlConnection = url.openConnection();

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }
        bufferedReader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return content.toString();
}

From source file:Main.java

public static JSONObject getAssetsJson(Context context, String jsonName) {
    try {// w  w w  .  j  a v  a  2s.c  o m
        InputStreamReader inputStreamReader = new InputStreamReader(context.getAssets().open(jsonName),
                "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        inputStreamReader.close();
        return new JSONObject(stringBuilder.toString());
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * read file to string list, a element of list is a line
 *
 * @param filePath/*from w  w  w.  j  a v  a  2  s .c o  m*/
 * @return if file not exist, return null, else return content of file
 * @throws IOException
 *             if an error occurs while operator BufferedReader
 */
public static List<String> readFileToList(String filePath) {
    File file = new File(filePath);
    List<String> fileContent = new ArrayList<String>();
    if (file == null || !file.isFile()) {
        return null;
    }

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent.add(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:org.sigmond.net.HttpUtils.java

public static String responseToString(HttpResponse response) throws IOException {
    InputStream in = response.getEntity().getContent();
    InputStreamReader ir = new InputStreamReader(in);
    BufferedReader bin = new BufferedReader(ir);
    String line = null;//from  ww  w  . ja va  2s .  co m
    StringBuffer buff = new StringBuffer();
    while ((line = bin.readLine()) != null) {
        buff.append(line + "\n");
    }
    bin.close();
    return buff.toString();
}

From source file:client.communication.SocketClient.java

public static void init() {
    BufferedReader buf;

    try {// w  ww .ja va 2  s  . co  m
        buf = new BufferedReader(new FileReader("src/ip.txt"));

        serverAddress = buf.readLine();

        //            System.out.println("Address: " + serverAddress);

        buf.close();
    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
}