Android Reader Read nextString(Reader r)

Here you can find the source of nextString(Reader r)

Description

Parse white space delimited String from Reader.

Parameter

Parameter Description
r Reader.

Return

next non white space String from r.

Declaration

public static String nextString(Reader r) 

Method Source Code

//package com.java2s;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;

public class Main {
    /**/*from w  ww  . jav  a2 s .c om*/
     * Parse white space delimited String from Reader.
     * @param r Reader.
     * @return next non white space String from r.
     */
    public static String nextString(Reader r) {
        StringWriter sw = new StringWriter();
        try {
            char buffer;
            int i;
            // skip white space
            while ((i = r.read()) > -1) {
                buffer = (char) i;
                if (!Character.isWhitespace(buffer)) {
                    sw.write(buffer);
                    break;
                }
            }
            // read non white space, ignore new lines ?
            while ((i = r.read()) > -1) {
                buffer = (char) i;
                if (Character.isWhitespace(buffer)) {
                    break;
                }
                sw.write(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }

    /**
     * Parse String with given delimiter from Reader.
     * @param c delimiter.
     * @param r Reader.
     * @return next String up to c from r.
     */
    public static String nextString(Reader r, char c) {
        StringWriter sw = new StringWriter();
        try {
            char buffer;
            int i;
            // read chars != c, ignore new lines ?
            while ((i = r.read()) > -1) {
                buffer = (char) i;
                if (buffer == c) {
                    break;
                }
                sw.write(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}

Related

  1. readFileReader(String filePath)
  2. readTextFile(String fileName)
  3. slurpLines(final Reader reader)
  4. slurpValidLines(final Reader reader)
  5. toList(Reader reader)
  6. nextString(Reader r, char c)
  7. nextPairedString(Reader r, char b, char c)