read Char from InputStream - Java java.io

Java examples for java.io:InputStream Read

Description

read Char from InputStream

Demo Code


//package com.java2s;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    private static char readChar(InputStream is) throws IOException {
        int ch1 = is.read();
        int ch2 = is.read();
        if ((ch1 | ch2) < 0)
            throw new EOFException();
        return (char) ((ch1 << 8) + (ch2 << 0));
    }/* w  w w.  j av a  2s  .  c  om*/

    public static String read(InputStream is) throws IOException {
        StringBuilder builder = new StringBuilder();
        char c;
        while ((c = readChar(is)) != 0)
            builder.append(c);
        return builder.toString();
    }
}

Related Tutorials