1、分別使用節(jié)點流FileInputStream、FileOutputStream和處理流:BufferedInputStread、BufferedOutputStread實現(xiàn)文本文件/圖片/視頻文件的復制。并比較二者在數(shù)據(jù)復制方面的效率。
代碼實測
package StringChuLi;
import org.junit.Test;
import java.io.*;
import java.time.format.FormatStyle;
import java.util.BitSet;
/**
*FileInputStream、FileOutputStream和
* 處理流:BufferedInputStread、BufferedOutputStread
* 實現(xiàn)文本文件/圖片/視頻文件的復制。并比較二者在數(shù)據(jù)復制方面的效率。
* @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){
// 創(chuàng)建節(jié)點流對象
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 {
//關(guān)閉資源
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){
// 創(chuàng)建節(jié)點流對象
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 {
//關(guān)閉資源
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){
// 創(chuàng)建節(jié)點流對象
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 {
//關(guān)閉資源
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){
// 創(chuàng)建節(jié)點流對象
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 {
//關(guān)閉資源
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
}
3、獲取文本上每個字符出現(xiàn)的次數(shù)
獲取文本上每個字符出現(xiàn)的次數(shù)并且存到Map集合中;將Map中的數(shù)據(jù)寫入文件
代碼實測
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);
//判斷這個字符是不是第一次出現(xiàn),
// 如果是第一次出現(xiàn)那就添加到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 {
// 關(guān)閉資源
if(bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}