本文内容
- Java利用IO流进行批量(某列)加解密小工具示意
- 此工具还有诸多不足和可以优化的地方,自用小工具,所以拓展性有待提高。
- 可能优化的方向:对于字符编码集的灵活设置等等
前置知识
- IO流
- Exception
- Scanner
- Eclipse/IDEA等工具
已经实现
- 通过控制台输入的值,返回加密后和解密后的字符串,对应单个字符串的加解密
- 通过IO流,输入结果文件和需要加/解密文件的路径与文件名,得到存放加/解密后字符串的文本。
- 实现方式为打成jar包形式,有些羞耻,就像是初入大学时候的课程设计一样简陋,但还算有效(特定环境下)
注意事项
- 加解密这里只是demo演示,不涉及具体加解密操作。
- 欢迎留下更好的解决思路和提出宝贵意见
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
网友评论