zoukankan      html  css  js  c++  java
  • Java 基础 (IO流练习-图片加解密,字符字数统计)

    图片加解密

    PicTest.java

    package com.klvchen.exer;
    
    import org.junit.Test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class PicTest {
    
        //图片加密
        @Test
        public void test1(){
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream("1.png");
                fos = new FileOutputStream("1secret.jpg");
    
                byte[] buffer = new byte[20];
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    //字节数组进行修改
                    //错误的
                    //for (byte b : buffer) {
                    //    b = (byte) (b ^ 5);
                    //}
    
                    //正确的
                    for (int i = 0; i < len; i++) {
                        buffer[i] = (byte) (buffer[i] ^ 5);
                    }
    
                    fos.write(buffer, 0 ,len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null)
                        fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
    
        }
    
        //图片解密
        @Test
        public void test2(){
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream("1secret.jpg");
                fos = new FileOutputStream("4.png");
    
                byte[] buffer = new byte[20];
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    //字节数组进行修改
                    //错误的
                    //for (byte b : buffer) {
                    //    b = (byte) (b ^ 5);
                    //}
    
                    //正确的
                    for (int i = 0; i < len; i++) {
                        buffer[i] = (byte) (buffer[i] ^ 5);
                    }
    
                    fos.write(buffer, 0 ,len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null)
                        fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    统计字符个数

    WordCount.java

    package com.klvchen.exer;
    
    import org.junit.Test;
    
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class WordCount {
        @Test
        public void testWordCount(){
            FileReader fr = null;
            BufferedWriter bw = null;
            try {
                //1.创建Map集合
                Map<Character, Integer> map = new HashMap<>();
    
                //2.遍历每一个字符,每一个字符出现的次数放到 map 中
                fr = new FileReader("dbcp.txt");
                int c = 0;
                while ((c = fr.read()) != -1) {
                    //int 还原 char
                    char ch = (char) c;
                    //判断 char 是否在 map 中第一次出现
                    if (map.get(ch) == null) {
                        map.put(ch, 1);
                    } else {
                        map.put(ch, map.get(ch) + 1);
                    }
                }
    
                //3.把map中数据存在文件count.txt
                //3.1 创建 Writer
                bw = new BufferedWriter( new FileWriter("count.txt"));
    
                //3.2 遍历 map,再写入数据
                Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
                for (Map.Entry<Character, Integer> entry : entrySet) {
                    switch (entry.getKey()) {
                        case ' ':
                            bw.write("空格=" + entry.getValue());
                            break;
                        case '	':
                            bw.write("tab键=" + entry.getValue());
                            break;
                        case '
    ':
                            bw.write("回车=" + entry.getValue());
                            break;
                        case '
    ':
                            bw.write("换行=" + entry.getValue());
                            break;
                        default:
                            bw.write(entry.getKey() + "=" + entry.getValue());
                            break;
                    }
                    bw.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bw != null)
                        bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fr != null)
                        fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  • 相关阅读:
    atom无法安装插件的解决方法之一
    css3伪类温故知新
    flex 布局笔记
    NPM 无法下载任何包的原因,解决方法
    flex align-content中的描述的“多根轴线的对齐方式”中的“多根轴线”到底是什么
    nodejs express 静态文件的路径
    当函数传入参数是引用类型的几种情况和现象。
    关于NODE NPM 输入命令后没反应的问题
    no input file specified
    获取form提交的返回值
  • 原文地址:https://www.cnblogs.com/klvchen/p/15484387.html
Copyright © 2011-2022 走看看