o7planning

Java StringReader Tutorial with Examples

  1. StringReader
  2. Examples

1. StringReader

StringReader class is used to read a String in style of a character input stream.
Basically, StringReader is a subclass of Reader. It has no other methods than methods inherited from its parent class. So you can read article about Reader to learn more about these methods.
StringReader constructor
public StringReader(String s)

2. Examples

Example: Read a text, listing the words that appear in text and the number of occurrences of each word.
  • Java StreamTokenizer
StringReaderEx2.java
package org.o7planning.stringreader.ex;
 
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

public class StringReaderEx2 {

    public static void main(String[] args) throws IOException {
        String s = "Apple Apricots Apricots Blackberries Apple";
        
        Reader reader = new StringReader(s);
        StreamTokenizer tokenizer = new StreamTokenizer(reader); // StreamTokenizer(Reader)
        
        // <String word, int count>
        Map<String, Integer> wordsMap = new HashMap<>();
        
        // Read and count the frequency for every word.
        while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
                int count;
                 
                if(wordsMap.containsKey(tokenizer.sval)) {
                    count = wordsMap.get(tokenizer.sval).intValue();
                    count++;
                }
                else {
                    count = 1;
                }
                wordsMap.put(tokenizer.sval, count);
            }
        }
        
        wordsMap.forEach((word, count) -> {
            System.out.println(word + " : " + count);
        });
    }
}
Output:
Apple : 2
Apricots : 2
Blackberries : 1
Example: Read a text with multiple lines, print lines that end with "B" and not begin with "#" (Comment line).
StringReaderEx3.java
package org.o7planning.stringreader.ex;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.StringReader;

public class StringReaderEx3 {

    public static void main(String[] args) {
        String students = //
                "# Students:\n" //
                        + "John P\n" //
                        + "Sarah M\n" //
                        + "# Sarah B\n" //
                        + "Charles B\n" //
                        + "Mary T\n" //
                        + "Sophia B\n";

        Reader reader = new StringReader(students);

        BufferedReader br = new BufferedReader(reader);

        br.lines() // return java.util.stream.Stream
                .filter(s -> !s.trim().startsWith("#")) // Line not start with #
                .filter(s -> s.endsWith("B")) // Line ends with B
                .forEach(System.out::println);

    }
}
Output:
Charles B
Sophia B

Java IO Tutorials

Show More