Android Reader Read nextString(Reader r, char c)

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

Description

Parse String with given delimiter from Reader.

Parameter

Parameter Description
c delimiter.
r Reader.

Return

next String up to c from r.

Declaration

public static String nextString(Reader r, char c) 

Method Source Code

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

public class Main {
    /**//from ww w.  j av a  2s.c  o m
     * 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. readTextFile(String fileName)
  2. slurpLines(final Reader reader)
  3. slurpValidLines(final Reader reader)
  4. toList(Reader reader)
  5. nextString(Reader r)
  6. nextPairedString(Reader r, char b, char c)