课后作业----给文件进行加密
编写一个文件加解密程序,通过命令行完成加解密工作
思路:因为读取文件是可以按照字节读取,或者字符读取,所有我们可以按照读字节后把字节改了或者把字符改了。
按照字节的:
代码:
package com.testHomework; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; public class FileAddCode { private static int numOfEncAndDec = 0x99; public static void main(String[] args) {
// 路径只能为觉得路径 File file1 = new File("D:\\jave编译器\\com.shangxuetang\\src\\abc.txt"); File file2 = new File("D:\\jave编译器\\com.shangxuetang\\aaa.txt"); //EnFile(file1,file2); DecFile(file2,file1); } // 文件的加密 public static void EnFile(File srcFile,File tarFile) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { InputStream is = new FileInputStream(srcFile); OutputStream os = new FileOutputStream(tarFile,true); bis = new BufferedInputStream(is); bos = new BufferedOutputStream(os); //byte[] flush = new byte[1024]; int len = -1; try { while((len=bis.read())!=-1) { //一个一个的读取文件内容 try { bos.write(len^numOfEncAndDec); // 对文件内容进行一个一个的加密 bos.flush(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(null!=bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 文件的加密 public static void DecFile(File encFile,File decFile) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { InputStream is = new FileInputStream(encFile); OutputStream os = new FileOutputStream(decFile,true); bis = new BufferedInputStream(is); bos = new BufferedOutputStream(os); //byte[] flush = new byte[1024]; int len = -1; try { while((len=bis.read())!=-1) { //一个一个的读取文件内容 try { bos.write(len^numOfEncAndDec); // 对文件内容进行一个一个的加密 bos.flush(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(null!=bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
运行结果:
字符
代码:
package com.testHomework; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.util.Arrays; public class FileAddCode2 { private static int numOfEncAndDec = 0x99; public static void main(String[] args) { File file1 = new File("D:\\jave编译器\\com.shangxuetang\\src\\abc.txt"); File file2 = new File("D:\\jave编译器\\com.shangxuetang\\aaa.txt"); /*try { EnFile(file1,file2); } catch (IOException e) { e.printStackTrace(); }*/ try { DecFile(file2,file1); } catch (IOException e) { e.printStackTrace(); } } // 文件的加密 public static void EnFile(File srcFile,File tarFile) throws IOException { Reader reader = null; Writer writer = null; try { reader = new FileReader(srcFile); writer = new FileWriter(tarFile,true); //byte[] flush = new byte[1024]; int len = -1; char[] flush = new char[1024]; try { while((len=reader.read(flush))!=-1) { //一个一个的读取文件内容 try { flush = addCode(flush,len); writer.write(flush,0,len); // 对文件内容进行一个一个的加密 writer.flush(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(null!=writer) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 文件的加密 public static void DecFile(File encFile,File decFile) throws IOException { Reader reader = null; Writer writer = null; try { reader = new FileReader(encFile); writer = new FileWriter(decFile,true); //byte[] flush = new byte[1024]; int len = -1; char[] flush = new char[1024]; try { while((len=reader.read(flush))!=-1) { //一个一个的读取文件内容 try { flush = deleteCode(flush,len); writer.write(flush,0,len); // 对文件内容进行一个一个的加密 writer.flush(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(null!=writer) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 对单个字符进行加密 public static char[] addCode(char[] temp,int len) { for(int i=0;i<len;i++) { temp[i] = (char) (temp[i]+5); } return temp; } // 对单个字符进行解密 public static char[] deleteCode(char[] temp,int len) { for(int i=0;i<len;i++) { temp[i] = (char) (temp[i]-5); } return temp; } }
运行结果: