例子:实现图片的加密操作:
图片以字节的方式去读,读的时候把一个字节读到b中,进行异或运算之后就相当于把原有的字节打乱了,图片就不能正常显示了,可以理解为加密操作
加密完之后可以进行解密,偷懒就不用缓冲流了,直接用节点流处理
import java.io.*;
//生成的加密文件的大小和原文件是一样的,但是生成的加密文件打不开
public class Test {
public static void main(String[] args) {
FileInputStream fi=null;//其实可以直接把文件的路径用String传给FileInputStream,即直接new FileInputSream("图.jpg");,FileInputStream操作的是文件,就算传给他的是字符串,还是会把他包装成文件
FileOutputStream fo=null;
try {
fi = new FileInputStream(new File("图.jpg"));
fo = new FileOutputStream(new File("图secrete.jpg"));
byte[] bytes=new byte[20];
int len;
while((len=fi.read(bytes))!=-1){
//对字节数据进行修改,执行加密操作,因为一次读进来了多个字节,要一个一个改,所以要用循环
//不能这么写,这是把bytes中的数据取出来赋给一个新的变量,对新的变量进行了修改,bytes中的数据没有变
/*
for(byte b:bytes){
b=(byte)(b^5);//b^5的类型是int
}
*/
for(int i=0;i<len;i++){
bytes[i]=(byte)(bytes[i]^5);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fi!=null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(fo!=null)
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//图片的解密
FileInputStream fi1=null;
FileOutputStream fo1=null;
try {
fi1= new FileInputStream(new File("图secrete.jpg"));
fo1 = new FileOutputStream(new File("图fun.jpg"));
byte[] bytes=new byte[20];
int len;
while((len=fi1.read(bytes))!=-1){
//解密操作还是这么干:要理解异或的性质:bytes[i]^5^5=bytes[i];
for(int i=0;i<len;i++) {
bytes[i] = (byte) (bytes[i] ^ 5);
}
//异或运算^:参加运算的两个数据,按二进制位进行“异或”运算。运算规则:0^0=0; 0^1=1; 1^0=1; 1^1=0;一个规律:m^n^n=m;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fi1!=null) {
try {
fi1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(fo1!=null)
fo1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
例题:获取文本上每个字符出现的个数
提示:遍历文本的每一个字符,字符(key)及出现的次数(value)保存在Map中,将Map中的数据(是内存中的)写入文件
package test0;
import org.junit.Test;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 思路:
* 1.遍历文本每一个字符
* 2.字符出现的次数存在Map中
*
* Map<Character,Integer> map = new HashMap<Character,Integer>();
* map.put('a',18);
* map.put('你',2);
*
* 3.把map中的数据写入文件
*
*/
public class WordCount {
/*
说明:如果使用单元测试,文件相对路径为当前module
如果使用main()测试,文件相对路径为当前工程
*/
@Test
public void testWordCount() {
FileReader fr = null;
BufferedWriter bw = null;
try {
//1.创建Map集合
Map<Character, Integer> map = new HashMap<Character, Integer>();
//2.遍历每一个字符,每一个字符出现的次数放到map中
fr = new FileReader("dbcp.txt");
int c;
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("wordcount.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'://\t表示tab 键字符
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();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关流
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}