当前位置:网站首页>缓冲流练习
缓冲流练习
2022-06-29 22:29:00 【小码哥呀】
1、使用IO流将图片的加密解密
public class PicTest {
//图片加密:对图片进行异或操作
@Test
public void test(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("bird.png");
fos = new FileOutputStream("bird-secret.png");
byte[] buffer = new byte[20];
int len;
while((len = fis.read(buffer))!=-1){
//对字节数组进行修改
for(int i=0;i<len; i++){
buffer[i] = (byte) (buffer[i] ^ 5);
}
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//图片解密:对图片进行异或的异或操作
@Test
public void test01(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("bird-secret.png");
fos = new FileOutputStream("bird4.png");
byte[] buffer = new byte[20];
int len;
while((len = fis.read(buffer))!=-1){
//对字节数组进行修改
for(int i=0;i<len; i++){
buffer[i] = (byte) (buffer[i] ^ 5);
}
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
加密后的图片无法打开
2、计算文本文件中字符出现的个数,并将结果输出到文本文件中
/* 1、遍历文本每一个字符
* 2、字出现的次数存在Map中
* Map<Character,Integer> map = new HashMap<Character,Integer>();
* map.put('a',18)
* map.put('你‘,2)
* 3、将map中的数据写入到文件中
* /
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("Hello.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("helloCount.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 '\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());
}
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
边栏推荐
- 把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
- Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
- math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
- 解题元宇宙,网络游戏中的多元通信方案
- STM32基础知识点
- GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
- Weekly Postgres world news 2022w25
- 5-1系统漏洞扫描
- 0. grpc environment setup
- Golang code specification sorting
猜你喜欢

Vs2013 how to make the program run on other computers

The soft youth under the blessing of devcloud makes education "smart" in the cloud

分布式消息中间件设计

Optional类的高级使用

AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践

解题元宇宙,网络游戏中的多元通信方案

Portable 4K audio and video conference terminal all-in-one machine with 8x digital zoom

Free PDF to word software sharing, these software must know!

If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview

云原生爱好者周刊:炫酷的 Grafana 监控面板集合
随机推荐
Cloud native enthusiast weekly: cool collection of grafana monitoring panels
leetcode 416. Partition equal subset sum partition equal subset sum (medium)
VS无法定位程序输入点于动态链接库
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
The soft youth under the blessing of devcloud makes education "smart" in the cloud
0. grpc环境搭建
在线文本数字识别列表求和工具
Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently
STM32 basic knowledge points
Polymorphism of laravel association model
Evaluation of powerful and excellent document management software: image management, book management and document management
Static keyword continuation, inheritance, rewrite, polymorphism
0. grpc environment setup
laravel 关联模型 多态关系
Ansible automatic operation and maintenance
R & D test time ratio, bug data analysis
为什么在局域网(ERP服务器)共享文件夹上拷贝文件时导致全局域英特网断网
Unity Pac Man games, maze implementation
The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]
Mysql database: the difference between drop, truncate and delete