IO输入输出案例题

视频参考:19-尚硅谷-Java语言高级-缓冲流课后练习2哔哩哔哩bilibili

案例一

1.实现图片的加密解密操作

package com.mokuiran.bufferedfile;

import java.io.*;

public class EncryptFile {
   public static void main(String[] args) {
       EncryptFile encryptFile = new EncryptFile();
//       encryptFile.TestEncrypt();

       encryptFile.TestDecrypt();
  }
   //对文件进行加密
   public void TestEncrypt(){
       FileInputStream fis = null;
       FileOutputStream fos = null;
       try {
           fis = new FileInputStream("1.jpg");
           fos = new FileOutputStream("Encrypt.jpg");

           byte[] bytes = new byte[1024];
           int len;
           while ((len = fis.read(bytes)) != -1){
               //加密操作的关键代码
               for (int i = 0; i < len; i++) {
                   bytes[i] = (byte) (bytes[i]^5);
              }
               fos.write(bytes,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();
      }

  }

   //对文件进行解密
   public void TestDecrypt(){
       FileInputStream fis = null;
       FileOutputStream fos = null;
       try {
           fis = new FileInputStream("Encrypt.jpg");
           fos = new FileOutputStream("Decrypt.jpg");

           byte[] bytes = new byte[1024];
           int len;
           while ((len = fis.read(bytes)) != -1){
               for (int i = 0; i < len; i++) {
                   bytes[i] = (byte) (bytes[i]^5);
              }
               fos.write(bytes,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();
      }

  }
}

案例二

2.获取文本上每个字符出现的次数

package com.mokuiran.bufferedfile;

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 WordCountFile {
   public static void main(String[] args) throws IOException {
       WordCountFile wordCountFile = new WordCountFile();
       wordCountFile.TestWordCount();

  }
   //计算每个字符出现的个数
   public void TestWordCount() throws IOException {
       //1.创建Map集合
       Map<Character,Integer> map = new HashMap<>();

       //2.遍历每一个字符,每一个字符出现的次数放到map中
       FileReader fr = new FileReader("hello.txt");
       int c;
       while ((c = fr.read()) != -1){
           //int 还原 char
           char ch = (char) c;
           //判断char是否在map中第一次出现
           //Map键值对的特性:键不能重复,值可以重复
           if (map.get(ch) == null){
               map.put(ch,1);
          }else {
               map.put(ch,map.get(ch) + 1);
          }
      }

       //3.把map中的数据存在文件当中
       //3.1 创建Writer
       BufferedWriter bw = new BufferedWriter(new FileWriter("WordCount.txt"));//建立新的文件

       //3.2 遍历map,再写入数据
       Set<Map.Entry<Character, Integer>> entries = map.entrySet();
       for (Map.Entry<Character,Integer> entry:entries){
           switch (entry.getKey()){
               case ' ':
                   bw.write("空格:"+entry.getValue());
                   break;
               case '\t':
                   bw.write("tab键:"+entry.getValue());
                   break;
               case  '\r':
                   bw.write("回车:"+entry.getValue());
                   break;
               case  '\n':
                   bw.write("换行:"+entry.getValue());
                   break;
               default:
                   bw.write(entry.getKey()+":"+entry.getValue());
                   break;

          }
           bw.newLine();
      }

       bw.close();
       fr.close();
  }
}
注:为了让代码看的清楚,这里只用了throws进行抛出异常,实际上运用try...catch...finally..才是最合适的。
 

 

posted @   默夔然  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示