Java CharArrayReader Tutorial with Examples
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.
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
Java IO Tutorials
- Java CharArrayWriter Tutorial with Examples
- Java FilterReader Tutorial with Examples
- Java FilterWriter Tutorial with Examples
- Java PrintStream Tutorial with Examples
- Java BufferedReader Tutorial with Examples
- Java BufferedWriter Tutorial with Examples
- Java StringReader Tutorial with Examples
- Java StringWriter Tutorial with Examples
- Java PipedReader Tutorial with Examples
- Java LineNumberReader Tutorial with Examples
- Java PrintWriter Tutorial with Examples
- Java IO Binary Streams Tutorial with Examples
- Java IO Character Streams Tutorial with Examples
- Java BufferedOutputStream Tutorial with Examples
- Java ByteArrayOutputStream Tutorial with Examples
- Java DataOutputStream Tutorial with Examples
- Java PipedInputStream Tutorial with Examples
- Java OutputStream Tutorial with Examples
- Java ObjectOutputStream Tutorial with Examples
- Java PushbackInputStream Tutorial with Examples
- Java SequenceInputStream Tutorial with Examples
- Java BufferedInputStream Tutorial with Examples
- Java Reader Tutorial with Examples
- Java Writer Tutorial with Examples
- Java FileReader Tutorial with Examples
- Java FileWriter Tutorial with Examples
- Java CharArrayReader Tutorial with Examples
- Java ByteArrayInputStream Tutorial with Examples
- Java DataInputStream Tutorial with Examples
- Java ObjectInputStream Tutorial with Examples
- Java InputStreamReader Tutorial with Examples
- Java OutputStreamWriter Tutorial with Examples
- Java InputStream Tutorial with Examples
- Java FileInputStream Tutorial with Examples
Show More