codestory

Die Anleitung zu Java CharArrayReader

  1. CharArrayReader
  2. Examples

1. CharArrayReader

CharArrayReader ist die Unterklasse von Reader. Wie der Name schon sagt, wird CharArrayReader verwendet, um eine Reihe von Zeichen im Stil eines Reader zu lesen.
CharArrayReader​ constructors
CharArrayReader​(char[] buf)     

CharArrayReader​(char[] buf, int offset, int length)
Die Methode von CharArrayReader:
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

Z.B: Lesen Sie ein Zeichenarray im Stil eines 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
Z.B: Verwenden Sie CharArrayReader und CharArrayWriter um nichtstellige Zeichen (digit) aus einem Text zu entfernen.
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
Beispiel: Entfernen Sie in einem mehrzeiligen Dokument die Leerzeichen am Anfang und Ende jeder Zeile, um einen neuen Text zu erstellen.
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

Die Anleitungen Java IO

Show More