当前位置:网站首页>缓冲流练习
缓冲流练习
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();
}
}
}
}
}
边栏推荐
- Ansible automatic operation and maintenance
- What are the software testing methods and technical knowledge points?
- Ansible自动化运维
- MySQL lock common knowledge points & summary of interview questions
- Touch key and key control corresponding LED status reversal
- An in-depth analysis of the election mechanism in kubernetes
- js函数相关的复习
- PhpSpreadsheet读写Excel文件
- STM32基础知识点
- MySQL lock common knowledge points & summary of interview questions
猜你喜欢

Nacos-配置中心基本使用

math_基本初等函数图型(幂函数/指数/对数/三角/反三角)

Hezhou air32f103cbt6 development board hands-on Report

Daily mathematics serial 54: February 23

Basic use of Nacos configuration center

一键式文件共享软件Jirafeau

AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
![The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]](/img/96/3e634c173c96082881286ba402a067.png)
The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]

IFLYTEK AI learning machine summer new product launch AI + education depth combination to create a new height of products

啃下大骨头——排序(一)
随机推荐
Number theory - division and blocking
MySQL lock common knowledge points & summary of interview questions
How ZABBIX 5.0 adds esxi6.7 to monitoring
Summer rainbow comes for dinner
地方/园区如何做好产业分析?
Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
js函数相关的复习
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
深入解析kubernetes中的选举机制
Polymorphism of laravel association model
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)
Spark cluster installation
5-1 system vulnerability scanning
The details of industry are all made by money and time
SYSTEMd debugging
Efficient implementation of dynamiccast with template function and specialization function
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
5-2Web应用程序漏洞扫描
MySQL lock common knowledge points & summary of interview questions