1、分别使用节点流FileInputStream、FileOutputStream和处理流:BufferedInputStread、BufferedOutputStread实现文本文件/图片/视频文件的复制。并比较二者在数据复制方面的效率。
代码实测
package StringChuLi;
import org.junit.Test;
import java.io.*;
import java.time.format.FormatStyle;
import java.util.BitSet;
/**
*FileInputStream、FileOutputStream和
* 处理流:BufferedInputStread、BufferedOutputStread
* 实现文本文件/图片/视频文件的复制。并比较二者在数据复制方面的效率。
* @Author: GenGen
* @Description: 6Ge
* @DateTime: 2022/4/11 14:36
**/
public class IO_Dome_LianXi01 {
public static void main(String[] args) {
JieDian("奖项.jpg","奖项3.jpg");
}
@Test
public static void JieDian(String a,String b){
// 创建节点流对象
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(a));
fos = new FileOutputStream(new File(b));
// 读入和写出操作
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1){
fos.write(bytes,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();
}
} }
}
public static void ChuLi(String a,String b){
// 创建节点流对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File(a)));
bos = new BufferedOutputStream(new FileOutputStream(new File(b)));
// 读入和写出操作
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
}
2、图片的加密和解密
package StringChuLi;
import com.sun.org.glassfish.gmbal.Description;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author: GenGen
* @Description: 6Ge
* @DateTime: 2022/4/11 15:04
**/
public class IO_Dome_LianXi02 {
public static void main(String[] args) {
// JiaMi("奖项.jpg","奖项加密.jpg");
JieMi("奖项加密.jpg","奖项4.jpg");
}
@Test
/**
* @Author: 有根
* @Description:
* @DateTime: 15:10 2022/4/11
* @Params: [a,b] a: 需要加密的文件名 b 加密后的文件名
* @Return [a,b]
*/
public static void JiaMi(String a,String b){
// 创建节点流对象
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(a));
fos = new FileOutputStream(new File(b));
// 读入和写出操作
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 {
//关闭资源
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
/**
* @Author: 有根
* @Description:
* @DateTime: 15:11 2022/4/11
* @Params: [a, b] a:要解密的文件 , b:解密后的文件名
* @Return [a, b]
*/
public static void JieMi(String a,String b){
// 创建节点流对象
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(a));
fos = new FileOutputStream(new File(b));
// 读入和写出操作
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 {
//关闭资源
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
}
3、获取文本上每个字符出现的次数
获取文本上每个字符出现的次数并且存到Map集合中;将Map中的数据写入文件
代码实测
package StringChuLi;
import org.junit.Test;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @Author: GenGen
* @Description: 6Ge
* @DateTime: 2022/4/11 15:21
**/
public class IO_Dome_LIanXi03 {
public static void main(String[] args) {
ZiFu("hello1.txt","newHello.txt");
}
@Test
/**
* @Author: 有根
* @Description:
* @DateTime: 15:38 2022/4/11
* @Params: [File , NewFile]
* File 需要读取的文件;newFile map集合存放的文件
* @Return [File, NewFile]
*/
static void ZiFu(String File,String NewFile){
BufferedWriter bw = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(File));
bw = new BufferedWriter(new FileWriter(NewFile));
Map<Character,Integer> map = new HashMap<>();
int c = 0;
while ((c = br.read()) != -1){
char ch = (char)(c);
//判断这个字符是不是第一次出现,
// 如果是第一次出现那就添加到map中
// 如果不是那就值加一
if (map.get(ch) == null){
map.put(ch,1);
}else {
map.put(ch,(map.get(ch) + 1));
}
}
// 遍历map集合 把
Set<Map.Entry<Character,Integer>> set = map.entrySet();
for (Map.Entry<Character,Integer> i : set){
switch (i.getKey()){
case ' '://空格
bw.write("空格="+i.getValue());
break;
case '\t'://tab键
bw.write("tab键=" + i.getValue());
break;
case '\r' ://回车键
bw.write("回车键=" + i.getValue());
break;
case '\n'://换行符
bw.write("换行符="+i.getValue());
break;
default:
bw.write(i.getKey()+"="+i.getValue());
break;
}
// 换行
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if(bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}