大话设计模式-继承OutputStream设计加密算法

import java.io.IOException;
import java.io.OutputStream;

/**
 * @ClassName: MyOutputStream
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/26 20:19
 */
public class MyOutputStream extends OutputStream {
    private OutputStream outputStream = null;

    public MyOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    @Override
    public void write(int b) throws IOException {
        //自定义的加密方式
        b += 2;
        //97+26
        if (b >= 123) {
            b -= 26;
        }
        this.outputStream.write(b);
    }
}
import java.io.*;

/**
 * @ClassName: Main
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/26 19:08
 */
public class Main {
    public static void main(String[] args) {
       
        try {
            DataOutputStream dataOutputStream =
                    new DataOutputStream(
                            new BufferedOutputStream(
                                    new MyOutputStream(
                                            new FileOutputStream("test.txt"))));
            dataOutputStream.write("abcdef".getBytes());
            dataOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务