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(char cbuf[], int off, int len) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("c:/test.txt");

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    char[] cbuf = new char[is.available()];

    br.read(cbuf, 2, 10);

    for (char c : cbuf) {
        if (c == (char) 0) {
            c = '*';
        }/*from  w w  w .  j  a  v a 2s.  c  om*/
        // prints characters
        System.out.print(c);
    }
}

From source file:Converter.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream(new File("input.txt"));
    BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

    FileOutputStream fos = new FileOutputStream(new File("output.txt"));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

    int len = 80;
    char buf[] = new char[len];

    int numRead;//w ww  .j  a  v  a  2s.  c  o  m
    while ((numRead = in.read(buf, 0, len)) != -1)
        out.write(buf, 0, numRead);
    out.close();
    in.close();
}

From source file:Main.java

public static String getContentFromFile(InputStream is) {
    char[] buffer = new char[1024];
    StringBuilder stringBuilder = new StringBuilder();
    try {// w  ww. j av  a 2  s .c  om
        InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while (bufferedReader.read(buffer, 0, 1024) != -1) {
            stringBuilder.append(buffer);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringBuilder.toString();
}

From source file:Main.java

public static String readFromFile(final File file) {
    final StringBuilder ret = new StringBuilder();
    try {/*from   w  w w  .ja v  a 2 s .  c o m*/
        final BufferedReader input = new BufferedReader(new FileReader(file), 4096);
        int len;
        final char buff[] = new char[4096];
        while ((len = input.read(buff, 0, 4096)) != -1) {
            ret.append(buff, 0, len);
        }
        input.close();
    } catch (final IOException exception) {
        exception.printStackTrace();
    }
    return ret.toString();
}

From source file:Main.java

public static String readToString(InputStream is) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    StringBuilder sb = new StringBuilder();
    char[] buffer = new char[8192];
    int read;//  ww w  .j av  a 2 s. co  m

    while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
        sb.append(buffer, 0, read);
    }

    return sb.toString();
}

From source file:Main.java

public static String toString(final InputStream is, final Charset charset) throws IOException {
    try {/*from w w w . j ava  2  s.c  o  m*/
        final BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
        final char[] buffer = new char[8192];
        final StringBuilder sb = new StringBuilder();
        int read;
        while ((read = reader.read(buffer, 0, buffer.length)) != -1) {
            sb.append(buffer, 0, read);
        }
        return sb.toString();
    } finally {
        is.close();
    }
}

From source file:org.dawb.passerelle.common.utils.SubstituteUtils.java

/**
 * Reads a UTF-8 text file with or without a BOM.
 * //from   ww w . j a v  a2  s. c o m
 * @param contents
 * @param variables
 * @return
 * @throws Exception
 */
public static String substitute(final InputStream contents, final Map<String, String> variables)
        throws Exception {

    if (contents == null)
        return null;
    try {
        BufferedReader ir = new BufferedReader(new InputStreamReader(contents, "UTF-8"));
        int c;
        StringBuilder expand = new StringBuilder();
        final char[] buf = new char[4096];
        while ((c = ir.read(buf, 0, 4096)) > 0) {

            if (expand.length() > 0) {
                if (expand.charAt(0) == FileUtils.BOM) {
                    expand.delete(0, 0);
                }
            }

            expand.append(buf, 0, c);
        }
        return SubstituteUtils.substitute(expand.toString(), variables);

    } finally {
        contents.close();
    }
}

From source file:com.machinepublishers.jbrowserdriver.Util.java

static String toString(InputStream inputStream, String charset) {
    try {/*from   w  w  w  .  j  a  va  2  s.  c o  m*/
        final char[] chars = new char[8192];
        StringBuilder builder = new StringBuilder(chars.length);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset), chars.length);
        try {
            for (int len; -1 != (len = reader.read(chars, 0, chars.length)); builder.append(chars, 0, len))
                ;
        } catch (EOFException | SSLProtocolException | ConnectionClosedException | SocketException e) {
        }
        return builder.toString();
    } catch (Throwable t) {
        return null;
    } finally {
        close(inputStream);
    }
}

From source file:MainClass.java

public static void Converter(String input, String output) {
    try {/*w ww.  j  ava  2s . co m*/
        FileInputStream fis = new FileInputStream(new File(input));
        BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

        FileOutputStream fos = new FileOutputStream(new File(output));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

        int len = 80;
        char buf[] = new char[len];

        int numRead;
        while ((numRead = in.read(buf, 0, len)) != -1)
            out.write(buf, 0, numRead);

        out.close();
        in.close();
    } catch (IOException e) {
        System.out.println("An I/O Exception Occurred: " + e);
    }
}

From source file:com.ruesga.rview.misc.AndroidHelper.java

public static String loadRawResourceAsStream(Context ctx) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = null;
    try {/* w  w w . j a  va  2  s . c o m*/
        reader = new BufferedReader(
                new InputStreamReader(ctx.getResources().openRawResource(R.raw.repositories)));
        char[] chars = new char[4096];
        int read;
        while ((read = reader.read(chars, 0, 4096)) != -1) {
            sb.append(chars, 0, read);
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
    return sb.toString();
}