Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

In this page you can find the example usage for java.io BufferedReader read.

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:com.moss.posixfifosockets.PosixFifoSocket.java

private static String read(InputStream in) throws IOException {
    StringBuilder text = new StringBuilder();
    char[] b = new char[1024];

    BufferedReader r = new BufferedReader(new InputStreamReader(in));

    for (int x = r.read(b); x != -1; x = r.read(b)) {
        text.append(b, 0, x);/*from w  w  w  .j  a va2 s. c  om*/
    }

    r.close();
    return text.toString();
}

From source file:revServTest.IoTTestForPOST.java

public static String readFileAsString(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);/*w ww .j av  a  2s.c  om*/
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

private static String readFileAsString(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);/*w ww.j av a 2s.  c  o m*/
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}

From source file:org.apache.tajo.util.FileUtil.java

public static String readTextFile(File file) throws IOException {
    StringBuilder fileData = new StringBuilder(1000);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    char[] buf = new char[1024];
    int numRead;/*from  ww  w  . j  a v  a  2  s .  c  o  m*/
    try {
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
    } finally {
        IOUtils.cleanup(null, reader);
    }
    return fileData.toString();
}

From source file:org.efaps.tests.program.Generic4VarArg.java

/**
 * Read file to string./*from   w ww  . ja va  2  s .  c o  m*/
 *
 * @param _file the file
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
// read file content into a string
public static String readFileToString(final File _file) throws IOException {
    final StringBuilder fileData = new StringBuilder(1000);
    final BufferedReader reader = new BufferedReader(new FileReader(_file));
    char[] buf = new char[10];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        final String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}

From source file:com.cloud.test.utils.SubmitCert.java

public static String readCert(String filePath) {
    try {/*ww w  . j a  va  2 s .c  o  m*/
        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);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    } catch (Exception ex) {
        s_logger.error(ex);
        return null;
    }
}

From source file:com.taobao.diamond.server.controller.CheckController.java

public static String url2str(String sURL, String encoding) {
    try {//  w  w  w .j ava2 s. c o m
        URL url = new URL(sURL);
        URLConnection conn = url.openConnection();
        if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpUrl = (HttpURLConnection) conn;
            httpUrl.getResponseCode();
        }
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
        String r = "";
        char[] cbuf = new char[1024];
        while (br.read(cbuf) > 0) {
            r += new String(cbuf);
        }
        return r;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.akquinet.android.androlog.reporter.PostReporter.java

/**
 * Reads an input stream and returns the result as a String.
 *
 * @param in/*from ww  w. j av a2s  .  c  o  m*/
 *            the input stream
 * @return the read String
 * @throws IOException
 *             if the stream cannot be read.
 */
public static String read(InputStream in) throws IOException {
    InputStreamReader isReader = new InputStreamReader(in, "UTF-8");
    BufferedReader reader = new BufferedReader(isReader);
    StringBuffer out = new StringBuffer();
    char[] c = new char[4096];
    for (int n; (n = reader.read(c)) != -1;) {
        out.append(new String(c, 0, n));
    }
    reader.close();

    return out.toString();
}

From source file:com.geoapi.api.server.services.implementations.WebViewService.java

/** @param filePath the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded
*//* ww w  .java2s .c  om*/
private static String readFileAsString(String filePath) {
    try {
        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);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return "Failed to open file.";
    }

}

From source file:com.geoapi.api.server.services.implementations.WebViewService.java

/** @param filePath the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded
*///  ww  w  .j a  va 2 s  .  com
private static String readFileAsString(File filePath) {
    try {
        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);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return "Failed to open file.";
    }

}