Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:StreamConverter.java

static String readInput() {

    StringBuffer buffer = new StringBuffer();
    try {//from w  ww .  j av  a  2  s  . c  om
        FileInputStream fis = new FileInputStream("test.txt");
        InputStreamReader isr = new InputStreamReader(fis, "UTF8");
        Reader in = new BufferedReader(isr);
        int ch;
        while ((ch = in.read()) > -1) {
            buffer.append((char) ch);
        }
        in.close();
        return buffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String toString(Reader reader) throws IOException {
    int charValue;
    StringBuffer sb = new StringBuffer(1024);
    while ((charValue = reader.read()) != -1) {
        sb.append((char) charValue);
    }/* w w w  . j  a  v  a2 s  . co m*/
    return sb.toString();
}

From source file:com.shahnami.fetch.Controller.JsonReader.java

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;//from  www.  j  av a2  s  .c  o  m
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString().replace(".ag", ".bypassed.me");
}

From source file:Main.java

public static String readerToString(Reader r) throws Exception {
    //Read into string
    StringBuffer buff = new StringBuffer();
    int c;/*from  ww w  . ja v a 2 s  .  c  o m*/
    while ((c = r.read()) != -1) {
        buff.append((char) c);
    }
    return buff.toString();
}

From source file:Main.java

/**
 * Reads all text up to next XML tag and returns it as a String.
 *
 * @return the String of the text read, which may be empty.
 *///from   ww  w .  j  a v a  2s . c om
public static String readUntilTag(Reader r) throws IOException {
    if (!r.ready()) {
        return "";
    }
    StringBuilder b = new StringBuilder();
    int c = r.read();
    while (c >= 0 && c != '<') {
        b.append((char) c);
        c = r.read();
    }
    return b.toString();
}

From source file:Main.java

public static String readerToString(final Reader r) throws Exception {
    // Read into string
    StringBuilder buff = new StringBuilder();
    int c;/*from   ww w . j a  va 2  s. c o  m*/
    while ((c = r.read()) != -1) {
        buff.append((char) c);
    }
    return buff.toString();
}

From source file:com.liferay.ci.json.JSONReaderImpl.java

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();

    int cp;/*from   ww  w  .  j a  v  a2s.  c  o m*/

    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }

    return sb.toString();
}

From source file:Main.java

private static void writeEscCData(Writer w, Reader input) throws IOException {
    int trigger = 0;
    w.write("<![CDATA[");
    int c;/* w w  w .ja v a 2  s .co m*/
    while ((c = input.read()) != -1) {
        if (c == ']') {
            w.write(']');
            trigger++;
        } else if ((c == '>') && (trigger >= 2)) {
            w.write("]]><![CDATA[>");
            trigger = 0;
        } else {
            w.write(c);
            trigger = 0;
        }
    }
    w.write("]]>");
}

From source file:Main.java

public static String readLineLimit(Reader reader, int limit) throws IOException {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < limit; i++) {
        int c = reader.read(); //Read in single character
        if (c == -1) {
            return ((sb.length() > 0) ? sb.toString() : null);
        }// ww w.  ja v a  2s.com

        if (((char) c == '\n') || ((char) c == '\r')) { //Found end of line, break loop.
            break;
        }

        sb.append((char) c); // String is not over and end line not found
    }

    return sb.toString(); //end of line was found.
}

From source file:Main.java

/**
 * Reads all text of the XML tag and returns it as a String.
 * Assumes that a '<' character has already been read.
 *
 * @param r The reader to read from/* www  .  j  a va 2s  . co  m*/
 * @return The String representing the tag, or null if one couldn't be read
 *         (i.e., EOF).  The returned item is a complete tag including angle
 *         brackets, such as <code>&lt;TXT&gt;</code>
 */
public static String readTag(Reader r) throws IOException {
    if (!r.ready()) {
        return null;
    }
    StringBuilder b = new StringBuilder("<");
    int c = r.read();
    while (c >= 0) {
        b.append((char) c);
        if (c == '>') {
            break;
        }
        c = r.read();
    }
    if (b.length() == 1) {
        return null;
    }
    return b.toString();
}