Java利用IO流进行批量(某列)加解密小工具示意

2019-06-25  本文已影响0人  iMUST_Clown

本文内容

前置知识

已经实现

注意事项

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class BatchEnDecryptTool {
    public static String ENTER="\n";
    public static void main(String[] args) {
        Scanner scanner=new java.util.Scanner(System.in);
        while(true) {
            System.out.println("欢迎使用xx加解密工具 version1.1");
            System.out.println("请选择");
            System.out.println("1.加密");
            System.out.println("2.解密");
            System.out.println("3.批量加密");
            System.out.println("4.批量解密");
            System.out.println("9.帮助");
            System.out.println("0.退出");
            System.out.println("请输入:");
            int menuSelect=scanner.nextInt();
            if(!((menuSelect==1)||(menuSelect==2)||(menuSelect==3)||(menuSelect==4)||(menuSelect==9)||(menuSelect==0))) {
                System.out.println("输入有误,请重新输入!");
                continue;
            }
            if(menuSelect==1) {
                System.out.println("请输入要加密的内容:");
                String beforeEncryptStr=scanner.next();
                System.out.println("加密后: ");
                System.out.println(encrypt(beforeEncryptStr));
            }else if(menuSelect==2) {
                System.out.println("请输入要解密的内容:");
                String beforeDecryptStr=scanner.next();
                System.out.println("解密后: ");
                System.out.println(decrypt(beforeDecryptStr));
            }else if(menuSelect==3) {
                System.out.println("请输入加密后文件存放路径及文件名:");
                String outPathAndFileName=scanner.next();
                System.out.println("请输入加密前文件存放路径及文件名:");
                String inPathAndFileName=scanner.next();
                try {
                    batchEncrypt(outPathAndFileName, inPathAndFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if(menuSelect==4) {
                System.out.println("请输入解密后文件存放路径及文件名:");
                String outPathAndFileName=scanner.next();
                System.out.println("请输入解密前文件存放路径及文件名:");
                String inPathAndFileName=scanner.next();
                try {
                    batchDecrypt(outPathAndFileName, inPathAndFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if(menuSelect==9) {
                System.out.println("帮助内容如下:");
            }else if(menuSelect==0) {
                System.out.println("感谢使用,已退出。");
                break;
            }
            
                
        }
    }
    /**
     * 解密,此处简略写
     * @param beforeDecryptStr
     * @return
     */
    private static  String decrypt(String beforeDecryptStr) {
        return "DECRYPT----"+beforeDecryptStr;
    }
    /**
     * 加密,此处简略写
     * @param beforeEncryptStr
     * @return
     */
    private static String encrypt(String beforeEncryptStr) {
        return "ENCRYPT----"+beforeEncryptStr;
    }
    /**
     * 批量加密
     * @param outPathAndFileName
     * @param inPathAndFileName
     * @throws IOException 
     */
    public static void batchEncrypt(String outPathAndFileName,String inPathAndFileName) throws IOException {
        int count=0;
        File fileIn=new File(inPathAndFileName);
        File fileOut=new File(outPathAndFileName);
        InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(fileIn));
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(fileOut));
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        String line_record=bufferedReader.readLine();
        while(null!=line_record&&line_record.length()>0) {
            String encrypted=encrypt(line_record);
            outputStreamWriter.append(encrypted);
            line_record=bufferedReader.readLine();
            count++;
            if(null!=line_record&&line_record.length()>0) {
                outputStreamWriter.append(ENTER);
            }
        }
        System.out.println("本次工加密 "+count+"条");
        System.out.println("加密后的文件路径为:"+outPathAndFileName);
        inputStreamReader.close();
        outputStreamWriter.close();
    }
    /**
     * 批量解密
     * @param outPathAndFileName
     * @param inPathAndFileName
     * @throws IOException 
     */
    public static void batchDecrypt(String outPathAndFileName,String inPathAndFileName) throws IOException {
        int count=0;
        File fileIn=new File(inPathAndFileName);
        File fileOut=new File(outPathAndFileName);
        InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(fileIn));
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(fileOut));
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        String line_record=bufferedReader.readLine();
        while(null!=line_record&&line_record.length()>0) {
            String decrypted=decrypt(line_record);
            outputStreamWriter.append(decrypted);
            line_record=bufferedReader.readLine();
            count++;
            if(null!=line_record&&line_record.length()>0) {
                outputStreamWriter.append(ENTER);
            }
        }
        System.out.println("本次工解密 "+count+"条");
        System.out.println("解密后的文件路径为:"+outPathAndFileName);
        inputStreamReader.close();
        outputStreamWriter.close();
    }
}

运行结果

1.单个字符串加密

1.png

2.单个字符串解密

2.png

3.批量字符串加密

3.png

4.批量字符串解密

4.png

5.帮助与退出

9和0.png

6.操作3和4后文件结果

3和4文件结果.png
上一篇下一篇

猜你喜欢

热点阅读