------------Encryption.java---------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Encryption {
/**
* 显示出该路径下所有文件名
*
* @param path
*/
public static void listFiles(String path) {
File f = new File(path);
if (!f.exists()) {
// 如果文件夹不存在,创建文件夹
f.mkdir();
return;
}
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
// 列出该文件夹下有效文件,文件夹除外
System.out.println(files[i].getName());
}
}
}
/**
* 为文件加密
*
* @param path
* @param file
* @throws IOException
*/
public static void encrypt(String path, String file) throws IOException {
File f = new File(path, file);
// 如果输入的文件不存在,或者输入的为文件夹,则不进入下面的流程。
if (!f.exists() || f.isDirectory()) {
System.out.println("文件不存在,或者不可读.");
return;
}
InputStream input = new FileInputStream(f);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
// 每次对一行数据操作
String value = reader.readLine();
while (value != null) {
char[] ch = value.toCharArray();
for (int i = 0; i < ch.length; i++) {
ch[i] = (char) (ch[i] + 1);
}
buffer.append(new String(ch) + "\r\n");
value = reader.readLine();
}
// 去掉多余的换行符
buffer.replace(buffer.lastIndexOf("\r\n"),buffer.length(),"");
System.out.println("----------加密完成-----------");
// 将加密后文件写入加密文件夹,名字默认为源文件文件名
OutputStream output = new FileOutputStream(f);
output.write(buffer.toString().getBytes());
output.close();
reader.close();
input.close();
}
/**
* 读取加密文件
*
* @param path
* @param file
* @throws IOException
*/
public static void decrypt(String path, String file) throws IOException {
File f = new File(path, file);
if (!f.exists() || f.isDirectory()) {
System.out.println("文件不存在,或者不可读.");
return;
}
InputStream input = new FileInputStream(f);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String value = reader.readLine();
while (value != null) {
char[] ch = value.toCharArray();
for (int i = 0; i < ch.length; i++) {
ch[i] = (char) (ch[i] - 1);
}
buffer.append(new String(ch) + "\r\n");
value = reader.readLine();
}
// 因为多加了一个换行符,需要去掉。
buffer.replace(buffer.lastIndexOf("\r\n"),buffer.length(),"");
OutputStream output = new FileOutputStream(f);
output.write(buffer.toString().getBytes());
System.out.println("-------------解密完成----------------");
output.close();
reader.close();
input.close();
}
}
--------------EncryptMain.java-----------------
import java.io.IOException;
import java.util.Scanner;
public class EncryptMain {
public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
String operation = "";
String opition = "";
while (true) {
System.out.println("-----------------文件加密,解密------------------");
System.out.println("1.加密 2.解密 3.退出");
int op = 0;
try {
opition = s.nextLine();
op = Integer.parseInt(opition);
} catch (Exception e) {
System.out.println("必须在1,2,3之中选择,请选择:");
opition = s.nextLine();
op = Integer.parseInt(opition);
}
switch (op) {
case 1:
do {
System.out.println("输入文件路径:");
String path = s.nextLine();
// 列出该文件夹下的文件名
Encryption.listFiles(path);
System.out.println("输入加密的文件名:");
String name = s.nextLine();
// 加密文件
Encryption.encrypt(path, name);
System.out.println("继续加密?(y/n)");
operation = s.nextLine();
} while (operation.equalsIgnoreCase("y"));
break;
case 2:
do {
System.out.println("输入文件路径:");
String path = s.nextLine();
// 列出该文件夹下的文件名
Encryption.listFiles(path);
System.out.println("输入解密的文件名:");
String name = s.nextLine();
Encryption.decrypt(path, name);
System.out.println("继续解密?(y/n)");
operation = s.nextLine();
} while (operation.equalsIgnoreCase("y"));
break;
case 3:
System.exit(0);
default:
System.out.println("无此选项,重新选择");
break;
}
}
}
}
温馨提示:内容为网友见解,仅供参考