Java 基础 (IO流练习-图片加解密,字符字数统计)

java

图片加解密

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 '\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();

}

} 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();

}

}

}

}

以上是 Java 基础 (IO流练习-图片加解密,字符字数统计) 的全部内容, 来源链接: utcz.com/z/394210.html

回到顶部