Java : StringReader with Examples

StringReader (Java SE 21 & JDK 21) with Examples.
You will find code examples on most StringReader methods.


Summary

A character stream whose source is a string.

Class diagram

try (final var reader = new StringReader("abc")) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 97 : a

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 98 : b

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 99 : c

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}
final var s = """
        abcd
        XYZ
        12345
        """;
try (final var reader = new BufferedReader(new StringReader(s))) {
    System.out.println(reader.readLine()); // abcd
    System.out.println(reader.readLine()); // XYZ
    System.out.println(reader.readLine()); // 12345
    System.out.println(reader.readLine()); // null
}

Fields declared in Reader

lock

Please see the link below.

Constructors

StringReader (String s)

Creates a new string reader.

try (final var reader = new StringReader("abcd")) {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
}

Methods

void close ()

Closes the stream and releases any system resources associated with it.

try (final var reader = new StringReader("abcd")) {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
}
// An example without a try-with-resources statement.
final var reader = new StringReader("abcd");
try {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
} finally {
    reader.close();
}

void mark (int readAheadLimit)

Marks the present position in the stream.

try (final var reader = new StringReader("abcde")) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    // A readAheadLimit is ignored.
    reader.mark(0);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1
}

boolean markSupported ()

Tells whether this stream supports the mark() operation, which it does.

try (final var reader = new StringReader("abcd")) {
    System.out.println(reader.markSupported()); // true
}

int read ()

Reads a single character.

try (final var reader = new StringReader("abc")) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 97 : a

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 98 : b

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 99 : c

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}

int read (char[] cbuf, int off, int len)

Reads characters into a portion of an array.

try (final var reader = new StringReader("abcdefg")) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [a,  ,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 2)); // 2
    System.out.println(Arrays.toString(cbuf)); // [b, c,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [d, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 4)); // 1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 5)); // -1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]
}
try (final var reader = new StringReader("abcde")) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 4, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  ,  , a]

    System.out.println(reader.read(cbuf, 3, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  , b, a]

    System.out.println(reader.read(cbuf, 2, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  , c, b, a]

    System.out.println(reader.read(cbuf, 1, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ , d, c, b, a]

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [e, d, c, b, a]
}
try (final var reader = new StringReader("abcdefg")) {
    final var cbuf = new char[3];

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [a, b, c]

    //reader.read(cbuf, 0, 4); // IndexOutOfBoundsException
    //reader.read(cbuf, 3, 1); // IndexOutOfBoundsException
}

boolean ready ()

Tells whether this stream is ready to be read.

try (final var reader = new StringReader("abc")) {
    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // b

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.ready()); // true
    System.out.println(reader.read()); // -1
}

void reset ()

Resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.

try (final var reader = new StringReader("abcde")) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    // A readAheadLimit is ignored.
    reader.mark(0);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1
}

long skip (long n)

Skips characters.

try (final var reader = new StringReader("abcdefghij")) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(1)); // 1
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.skip(2)); // 2
    System.out.printf("%c%n", reader.read()); // f

    System.out.println(reader.skip(3)); // 3
    System.out.printf("%c%n", reader.read()); // j

    System.out.println(reader.skip(1)); // 0
    System.out.println(reader.read()); // -1
}
try (final Reader reader = new StringReader("abcde")) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(10)); // 4
    System.out.println(reader.read()); // -1
}

Methods declared in Reader

nullReader, read, read, transferTo

Please see the link below.


Related posts

To top of page