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 contentFromFixture(String fixtureName) {
    BufferedReader br;
    StringBuilder sb = new StringBuilder();
    try {//from www .ja v  a  2  s .  co  m
        br = new BufferedReader(
                new FileReader("/Users/chang/eclipse/workspace/BasicBlock/src/fixtures/" + fixtureName));

        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.reuters.Reuters21578TxtReader.java

/**
 * Read a Reuters text file into a Map// w w  w  .  j a va  2  s . co m
 *
 * @param reutersFile a Reuters text file
 * @return a Map with keys {@code dateline}, {@code title}, and {@code text}
 * @throws IOException if the file cannot be read
 */
private static Map<String, String> readFile(File reutersFile) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(reutersFile));
    String dateline = reader.readLine();
    reader.readLine(); // skip empty line
    String title = reader.readLine();
    reader.readLine(); // skip empty line
    String text = reader.readLine();
    reader.close();

    Map<String, String> doc = new HashMap<>();
    doc.put("title", title);
    doc.put("dateline", dateline);
    doc.put("text", text);

    return doc;
}

From source file:com.springsource.insight.plugin.ldap.LdapOperationCollectionAspectTestSupport.java

private static Collection<Map<String, Set<String>>> readLdifEntries(String location) throws IOException {
    ClassLoader cl = ClassUtil.getDefaultClassLoader();
    InputStream in = cl.getResourceAsStream(location);
    assertNotNull("No LDIF input at " + location, in);

    BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
    try {//from  w  w w.j  a va 2  s.co m
        return readLdifEntries(rdr);
    } finally {
        rdr.close();
    }
}

From source file:emily.util.YTUtil.java

/**
 * @param videocode youtubecode//ww  w . ja  v a  2s.  c o m
 * @return whats in the <title> tag on a youtube page
 */
public static String getTitleFromPage(String videocode) {
    String ret = "";
    try {
        URL loginurl = new URL("https://www.youtube.com/watch?v=" + videocode);
        URLConnection yc = loginurl.openConnection();
        yc.setConnectTimeout(10 * 1000);
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        StringBuilder input = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            input.append(inputLine);
        in.close();
        int start = input.indexOf("<title>");
        int end = input.indexOf("</title>");
        ret = input.substring(start + 7, end - 10);
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
    return StringEscapeUtils.unescapeHtml4(ret);
}

From source file:Main.java

public static String getFileOutputString(String path, String charset) {
    try {/*from   w  w w.ja v  a2  s . co  m*/
        File file = new File(path);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(file), charset), 8192);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append("\n").append(line);
        }
        bufferedReader.close();
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String getStringFromInputStream(InputStream is) {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line = null;//from  w  w w  . ja  va  2 s  . c  o  m

    try {
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
    } catch (IOException e) {
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
            }
        }
    }
    return sb.toString();
}

From source file:com.meetingninja.csse.database.BaseDatabaseAdapter.java

protected static String getServerResponse(URLConnection connection) throws IOException {
    // Read server response
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;// w  ww .  j  a  va 2 s. c o  m
    StringBuffer response = new StringBuffer();

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

    // return page contents
    return response.toString();
}

From source file:ca.sparkera.adapters.mainframe.CobolSerdeUtils.java

public static String getLayoutFor(InputStream stream) {
    StringBuilder out = new StringBuilder();
    try {//from   w  ww  .j a  v  a2  s  .c  om
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        reader.close();
    } catch (IOException e) {
        throw new RuntimeException("Failed to parse Cobol layout", e);
    }
    return getLayoutFor(out.toString());
}

From source file:Main.java

/**
 * read file to string list, a element of list is a line
 * /*w  w w  .j av  a  2s .  c om*/
 * @param filePath
 * @param charsetName The name of a supported {@link java.nio.charset.Charset </code>charset<code>}
 * @return if file not exist, return null, else return content of file
 * @throws RuntimeException if an error occurs while operator BufferedReader
 */
public static List<String> readFileToList(String filePath, String charsetName) {
    File file = new File(filePath);
    List<String> fileContent = new ArrayList<String>();
    if (file == null || !file.isFile()) {
        return null;
    }

    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        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:com.microsoft.office365.connectmicrosoftgraph.ConnectUnitTests.java

@BeforeClass
public static void getAccessTokenUsingPasswordGrant()
        throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, JSONException {
    URL url = new URL(TOKEN_ENDPOINT);
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

    String urlParameters = String.format(
            "grant_type=%1$s&resource=%2$s&client_id=%3$s&username=%4$s&password=%5$s", GRANT_TYPE,
            URLEncoder.encode(Constants.MICROSOFT_GRAPH_API_ENDPOINT_RESOURCE_ID, "UTF-8"), clientId, username,
            password);//  ww  w  .  j  a  v a2  s.c  o m

    connection.setRequestMethod(REQUEST_METHOD);
    connection.setRequestProperty("Content-Type", CONTENT_TYPE);
    connection.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes("UTF-8").length));

    connection.setDoOutput(true);
    DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
    dataOutputStream.writeBytes(urlParameters);
    dataOutputStream.flush();
    dataOutputStream.close();

    connection.getResponseCode();

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

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

    JsonParser jsonParser = new JsonParser();
    JsonObject grantResponse = (JsonObject) jsonParser.parse(response.toString());
    accessToken = grantResponse.get("access_token").getAsString();
}