Design an input stream, the function of this class is to convert uppercase letters in the file to lowercase, lowercase to uppercase, and the numbers remain unchanged when reading the file

A decoration class can be implemented by inheriting the abstract decorator class (FilterInputStream), and this function can be realized by calling some methods provided by the InputStream class or its subclasses and adding logical judgments.

import java.io.*;

/**
 * Created by Administrator on 2018/4/25 0025.
 */
public class MyOwnInputStream extends FilterInputStream{
    public static void main(String[] args) {
        int c;
        try {
            InputStream is = new MyOwnInputStream(new FileInputStream("D:/test.txt"));
            while ((c = is.read()) >= 0) {
                System.out.println((char) c);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public MyOwnInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        int c = super.read();
        if (c != -1) {
            if (Character.isLowerCase((char)c)) {
                return Character.toUpperCase((char) c);
            }
            else if (Character.isUpperCase((char)c)) {
                return Character.toLowerCase((char) c);
            }
            else {
                return c;
            }
        } else {
            return -1;
        }

    }

}

References Java Programmer Interview Written Test Collection P117

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326052538&siteId=291194637