o7planning

Java CharArrayReader Tutorial

View more Tutorials:

1- CharArrayReader

CharArrayReader is a subclass of Reader. True to its name, CharArrayReader is used to read an array of characters in the style of a Reader.
  • TODO Link!
CharArrayReader​ constructors
CharArrayReader​(char[] buf)     

CharArrayReader​(char[] buf, int offset, int length)
CharArrayReader Methods:
public void close()  

public void mark​(int readAheadLimit)  
public boolean markSupported()  

public static Reader nullReader()  

public int read()  
public int read​(char[] cbuf)  
public int read​(char[] cbuf, int off, int len)  
public int read​(CharBuffer target)

public boolean ready()  
public void reset()  
public long skip​(long n)  
public long transferTo​(Writer out)

2- Examples

Example: Read a character array in the style of a Reader:
CharArrayReaderEx1.java
package org.o7planning.chararrayreader.ex;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderEx1 {

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

        char[] charArray = new char[] { 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'e', 'x', 't' };

        CharArrayReader reader = new CharArrayReader(charArray);
        
        int code;
        while((code = reader.read()) != -1) {
            char ch = (char) code;
            System.out.println(ch);
        }
    }
}
Output:
T
h
i
s
 
i
s
 
t
e
x
t
Example: Use CharArrayReader and CharArrayWriter to remove non-digit characters from a text.
CharArrayReaderEx2.java
package org.o7planning.chararrayreader.ex;

import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayReaderEx2 {

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

        String originText = "Log20210217.txt";
        
        char[] charArray = originText.toCharArray();

        CharArrayReader reader = new CharArrayReader(charArray);
        CharArrayWriter writer = new CharArrayWriter();
        
        int code;
        while((code = reader.read()) != -1) {
            // '0', '1', ... '9'
            if(code >= 48 && code <= 57) {
                writer.write(code);
            }
        }
        char[] newCharArray = writer.toCharArray(); // ['2','0','2','1','0','2','1','7']
        System.out.println(newCharArray);
        
        String newText = writer.toString();
        System.out.println(newText);
    }
}
Output:
20210217
20210217
Example: A multi-line document, remove white spaces at the beginning and end of each line to create a new text.
CharArrayReaderEx3.java
package org.o7planning.chararrayreader.ex;

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderEx3 {

    public static void main(String[] args) throws IOException {
        String originText = "  One \n Two \t\n \t\t Three ";
        
        System.out.println(originText);
        System.out.println("-----------");
        
        String newText = trimLines(originText);
        System.out.println(newText);
    }  
    
    private static String trimLines(String string) throws IOException {
        CharArrayReader reader = new CharArrayReader(string.toCharArray());
        
        BufferedReader in = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder(string.length());
        
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line.trim()).append('\n');
        }
        return sb.toString();
      }
}
Output:
  One
 Two     
          Three
-----------
One
Two
Three

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.