日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java实现对文件加解密操作

發布時間:2024/9/27 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现对文件加解密操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

源文件:

加密后的文件:

解密后的文件:

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(newFileOutputStream(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(newFileOutputStream(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) {//windowsreturn dealFileName.substring(dealFileName.lastIndexOf("\\") + 1);} else {//linuxreturn dealFileName.substring(dealFileName.lastIndexOf("/") + 1);}} }

總結

以上是生活随笔為你收集整理的java实现对文件加解密操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。