Example usage for java.io StringReader read

List of usage examples for java.io StringReader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:Main.java

public static void main(String[] argv) throws IOException {
    StringReader stringReader = new StringReader("java2s.com");
    System.out.println(stringReader.read());

    stringReader.close();/*from  ww w  .  j a v a 2 s.c  o  m*/
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    StringReader in2 = new StringReader("a bc ddd");
    int c;/* www .  j  a  v  a2s. co m*/
    while ((c = in2.read()) != -1)
        System.out.print((char) c);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(stdin.readLine());
    BufferedReader in = new BufferedReader(new FileReader("Main.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
        s2 += s + "\n";
    in.close();//from ww  w.  j a  v a2  s . c  o  m
    StringReader in1 = new StringReader(s2);
    int c;
    while ((c = in1.read()) != -1)
        System.out.print((char) c);
    BufferedReader in2 = new BufferedReader(new StringReader(s2));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
    int lineCount = 1;
    while ((s = in2.readLine()) != null)
        out1.println(lineCount++ + ": " + s);
    out1.close();
}

From source file:Main.java

public static void main(String[] argv) throws IOException {
    StringReader stringReader = new StringReader("java2s.com");
    stringReader.skip(2);/*from ww w .j  a v  a 2  s  .c  o m*/
    System.out.println(stringReader.read());

    stringReader.close();
}

From source file:Main.java

public static void main(String[] argv) throws IOException {
    StringReader stringReader = new StringReader("java2s.com");

    stringReader.mark(2);// w ww.j  a v a  2s . co  m

    System.out.println(stringReader.markSupported());

    System.out.println(stringReader.read());

    stringReader.reset();

    stringReader.close();
}

From source file:StringIOApp.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//  ww w.  j a  v  a 2 s .c om
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
    StringReader inStream;
    inStream = new StringReader(outStream.toString());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}

From source file:Main.java

public static String readString(StringReader sr, char delimiter) {
    try {// w w  w . j  a  v a  2s.com
        StringBuilder sb = new StringBuilder();
        char c;
        while ((c = (char) sr.read()) != (char) -1) {
            if (c == '\\') {
                // This is an escape character. Jump past to the next char.
                sb.append((char) sr.read());
            } else if (c == delimiter) {
                break;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    } catch (IOException e) {
        // Cannot happen as there is no IO here - just a read from a string
        throw new RuntimeException("Unexpected IOException", e);
    }

}

From source file:Main.java

/**
 * Takes a single line command as a string and breaks it up in tokens
 * acceptable for the java.lang.ProcessBuilder.ProcessBuilder
 * @param command Complete command as a single string
 * @return Array of strings that are acceptable tokens for ProcessBuilder
 * @throws Exception /*www . jav a  2 s  .  c  o m*/
 */
static public List<String> breakUpCommand(String command) throws Exception {
    try {
        List<String> commandTokens = new Vector<String>();

        StringBuilder currentToken = null;
        boolean isTokenQuoted = false;
        StringReader sr = new StringReader(command);
        int b = sr.read();
        while (b >= 0) {
            char c = (char) b;

            if (null == currentToken) {
                // At token is not in progress

                // Skipping spaces
                if (' ' == c || '\t' == c) {

                } else if ('"' == c) {
                    // Starting a quoted token
                    currentToken = new StringBuilder();
                    //currentToken.append(c);
                    isTokenQuoted = true;
                } else {
                    // Starting a non-quoted token
                    currentToken = new StringBuilder();
                    currentToken.append(c);
                    isTokenQuoted = false;
                }

            } else if (isTokenQuoted) {
                // A quoted token is in progress. It ends with a quote
                if ('"' == c) {
                    //currentToken.append(c);
                    String token = currentToken.toString();
                    currentToken = null;
                    commandTokens.add(token);

                } else {
                    // Continuation
                    currentToken.append(c);
                }

            } else {
                // A non-quoted token is in progress. It ends with a space
                if (' ' == c || '\t' == c) {
                    String token = currentToken.toString();
                    currentToken = null;
                    commandTokens.add(token);

                } else {
                    // Continuation
                    currentToken.append(c);
                }
            }

            b = sr.read();
        }

        if (null != currentToken) {
            String token = currentToken.toString();
            commandTokens.add(token);
        }

        return commandTokens;

    } catch (IOException e) {
        throw new Exception("Error while breaking up command into tokens: " + command, e);
    }
}

From source file:BasicUUID.java

/**
 * Read a long value, msb first, from its character representation in the
 * string reader, using '-' or end of string to delimit.
 *///from  w  w  w.  j av a  2 s.  c  o m
private static long readMSB(StringReader sr) {
    long value = 0;

    try {
        int c;
        while ((c = sr.read()) != -1) {
            if (c == '-')
                break;
            value <<= 4;

            int nibble;
            if (c <= '9')
                nibble = c - '0';
            else if (c <= 'F')
                nibble = c - 'A' + 10;
            else
                nibble = c - 'a' + 10;
            value += nibble;
        }
    } catch (Exception e) {
    }

    return value;
}

From source file:fi.uta.infim.usaproxylogparser.UsaProxyHTTPTrafficLogParser.java

/**
 * Converts a UsaProxy DOM path to an XPath expression
 * @param usaProxyPath usaproxy dom path
 * @return XPath expression as string//  w  w w.ja  v a2  s  .  c o  m
 * @throws IOException
 * @throws XPathExpressionException
 */
static String usaProxyDOMPathToXPath(String usaProxyPath) throws IOException, XPathExpressionException {
    String path = "";
    StringReader reader = new StringReader(usaProxyPath);
    while (reader.ready()) {
        int depth = 0; // Index of the child element
        int currentChar;
        String prefix = "0";
        while (Character.isDigit(currentChar = reader.read())) {
            prefix += String.valueOf((char) currentChar);
        }
        if (currentChar == -1)
            break;
        depth += Integer.parseInt(prefix) * 26;
        depth += currentChar - ((int) 'a') + 1; // assuming ascii charset
        path += "/*[" + String.valueOf(depth) + "]";
    }

    return path;
}