源文件:

java实现对文件加解密操作_指定目录


加密后的文件:

java实现对文件加解密操作_指定目录_02


解密后的文件:

java实现对文件加解密操作_路径和_03

package com.gblfy.test;

import java.io.*;

/**
* java 实现对文件加解密的方法
*
* @author gblfy
* @date 2020-12-08
*/
public class IOSercet {

//获取系统类型
private static String OS = System.getProperty("os.name").toLowerCase();
//加密后的路径
public static final String secretFilePath = "I:/666/";
//解密后的路径
public static final String decrypFilePath = "I:/777/";

public static void main(String[] args) {

//1.测试批量加密(源文件路径和加密后的文件路径不一样)
getAllFileNameSecret("I:/555/");
//2.测试批量加密(加密文件的源路径和解密后的路径不一样)
getAllFileNameDecrypt("I:/666/");
}

/**
* 批量解密
* 适用于加密文件的路径和解密后的路径不一样的业务场景
* 获取指定目录下的所有文件及子类文件夹下的所有文件
*
* @param path 指定需要解密的目录
*/
public static void getAllFileNameSecret(String path) {
File file = new File(path);
File[] tempList = file.listFiles();

for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文 件:" + tempList[i]);
secret(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
System.out.println("文件夹:" + tempList[i]);
getAllFileNameSecret(tempList[i].getAbsolutePath());
}
}
return;
}

/**
* 批量解密
* 适用于加密文件的路径和解密后的路径不一样的业务场景
* 获取指定目录下的所有文件及子类文件夹下的所有文件
*
* @param path 指定需要解密的目录
*/
public static void getAllFileNameDecrypt(String path) {
File file = new File(path);
File[] tempList = file.listFiles();

for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文 件:" + tempList[i]);
decrypt(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
System.out.println("文件夹:" + tempList[i]);
getAllFileNameDecrypt(tempList[i].getAbsolutePath());
}
}
return;
}

/**
* 批量加密
* 适用于源路径和加密后的路径不一样的业务场景
*
* @param srcFileName 源文件名称
*/
public static void secret(String srcFileName) {
long a = 0;
long b = 0;
createDir(secretFilePath);
String secretFileName = getFileName(srcFileName);
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFileName));
BufferedOutputStream bos = new BufferedOutputStream(new
FileOutputStream(secretFilePath + secretFileName));
int n;
a = System.currentTimeMillis();
while ((n = bis.read()) != -1) {
bos.write(n + 1);
}
b = System.currentTimeMillis();
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("加密拷贝成功!");
System.out.println("加密用时:" + (b - a) + "ms");
}


/**
* 批量解密
* 适用于加密文件的路径和解密后的路径不一样的业务场景
*
* @param secretFileName 加密的文件名称
*/
public static void decrypt(String secretFileName) {
long a = 0;
long b = 0;
try {
createDir(decrypFilePath);
String decryptFileName = getFileName(secretFileName);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(secretFileName));
BufferedOutputStream bos = new BufferedOutputStream(new
FileOutputStream(decrypFilePath + decryptFileName));
int n;
a = System.currentTimeMillis();
while ((n = bis.read()) != -1) {
bos.write(n - 1);
}
b = System.currentTimeMillis();
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("解密拷贝成功!");
System.out.println("解密用时:" + (b - a) + "ms");
}

/**
* 目录不存在,则批量创建
*
* @param fileDir
*/
public static void createDir(String fileDir) {
File file = new File(fileDir);
//如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
System.out.println("//不存在");
file.mkdirs();
}
}

/**
* 文件名称处理
*
* @param dealFileName
* @return
*/
public static String getFileName(String dealFileName) {
if (OS.indexOf("windows") >= 0) {
//windows
return dealFileName.substring(dealFileName.lastIndexOf("\\") + 1);
} else {
//linux
return dealFileName.substring(dealFileName.lastIndexOf("/") + 1);
}
}
}