Java FilterReader extend to create custom Reader

Description

Java FilterReader extend to create custom Reader

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;

public class Main {
  public static void main(String[] args) throws Exception {
    String fileName = "Main.java";
    MyReader lcr = new MyReader(new FileReader(fileName));
    int c = -1;/*from w w  w .ja  va 2  s. c o m*/
    while ((c = lcr.read()) != -1) {
      System.out.print((char) c);
    }
    lcr.close();
  }
}

class MyReader extends FilterReader {
  public MyReader(Reader in) {
    super(in);
  }

  @Override
  public int read(char[] cbuf, int off, int len) throws IOException {
    int count = super.read(cbuf, off, len);
    if (count != -1) {
      int limit = off + count;
      for (int i = off; i < limit; i++) {
        cbuf[i] = Character.toLowerCase(cbuf[i]);
      }
    }
    return count;
  }
}



PreviousNext

Related