java小白都能看懂的文件装x操作

最近学了java的文件操作,了解到都是字节和字符的操作,于是突然萌生了一个想法,搞个文件加密和解密怎么样,于是便有了下面这些代码

1.文件加密/解密

2.视频加密/解密

3.有任务条实时进度的加密过程…

1.文件加密

因为gbk中文是两个字节,首先我们只要知道如何使用字节输入输出流,和文件增删重命名的基本操作就可以了,上代码,

运行一次加密,运行第二次解密…

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class MainTest {
	public static void main(String[] args) throws IOException {
		File fileOld = new File("d:\\xxs.txt");
		File fileNew = new File("d:\\randomname.txt");
		//创建一个文件并添加内容,博主怕有些人懒得不想去创建,这是针对中文的加密,
		//对于英文的加密,看了博主的文章,相信你能够自己完成!!
		createFile(fileOld);
		//两位一读取byte字节,反转两个字节再输出到新文件
		encrypt(fileOld,fileNew);
		//删除原文件,把改变后的字节文件命名为以前的文件名
		renameTo(fileOld,fileNew);
	}
	public static void renameTo(File fileOld,File fileNew) {
		fileOld.delete();
		fileNew.renameTo(fileOld);
	}
	public static void encrypt(File fileOld,File fileNew) throws IOException {
		FileOutputStream fos = new FileOutputStream(fileNew);
		FileInputStream fis = new FileInputStream(fileOld);
		byte[] bytes = new byte[2];
		int len = 0;
		while((len=fis.read(bytes))!=-1) {
			byte temp = bytes[0];
			bytes[0] = bytes[1];
			bytes[1] = temp;
			fos.write(bytes,0,len);
		}
		fos.close();
		fis.close();
	}
	
	public static void createFile(File f) throws IOException {
		if(f.exists()) {
			System.out.println("你已经创建过了,逗我玩呢....");
			return;
		}else {
			System.out.println(f.createNewFile());
			FileOutputStream fos = new FileOutputStream(f);
			fos.write("程序员 不容sout易 def掏空\r\n身体 养家print(照顾你)\r\n".getBytes());
			fos.close();
		}
	}
}

在这里插入图片描述
在这里插入图片描述

2.视频加密/解密过程,由于我做测试的视频两百多M,就不得不加入一个新的知识,BufferedInputStream 和 BufferedOutputStream,也挺简单的,相信大家都是能够自学完成的,然后废话不多说,上代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainTest {
	public static void main(String[] args) throws IOException {
		File fileOld = new File("d:\\1.mp4");
		File fileNew = new File("d:\\randomname.mp4");
		//对视频的加密或者解密,视频需要自己去找了,尽量找个几百m的就行,后面体验第三个demo结果
		encryptFile(fileOld,fileNew);
		//重命名加密或解密后的文件和删除原文件
		renameTo(fileOld,fileNew);
	}
	public static void renameTo(File fileOld,File fileNew) {
		fileOld.delete();
		fileNew.renameTo(fileOld);
	}
	public static void encryptFile(File fileOld,File fileNew) throws IOException {
		FileInputStream fis = new FileInputStream(fileOld);
		FileOutputStream fos = new FileOutputStream(fileNew);
		BufferedInputStream bis = new BufferedInputStream(fis);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		byte[] bytes = new byte[2];
		int len = 0;
		while((len=bis.read(bytes))!=-1) {
			byte temp = bytes[0];
			bytes[0] = bytes[1];
			bytes[1] = temp;
			bos.write(bytes);
		}
		bis.close();
		bos.close();
	}
}
3.上述的代码已经可以实现文件和视频的加密解密过程了,但是作为一个想要装x的程序员怎么能够满足于此,来我们对第二个视频加密或加密的代码进行改进,还是那句话,上代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainTest {
	public static void main(String[] args) throws IOException {
		File fileOld = new File("d:\\1.mp4");
		File fileNew = new File("d:\\randomname.mp4");
		encryptFile(fileOld,fileNew);
		renameTo(fileOld,fileNew);
		System.out.println("文件已加密//解密.....");
	}
	public static void renameTo(File fileOld,File fileNew) {
		System.out.println("原文件删除状况-->"+fileOld.delete());
		System.out.println("新文件命名状况-->"+fileNew.renameTo(fileOld));
	}
	public static void encryptFile(File fileOld,File fileNew) throws IOException {
		FileInputStream fis = new FileInputStream(fileOld);
		FileOutputStream fos = new FileOutputStream(fileNew);
		System.out.println("旧文件已就绪.....\r\n新文件已就绪.....");
		BufferedInputStream bis = new BufferedInputStream(fis);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		System.out.println("需要解密//加密的视频大小为-->"+fis.available()+"字节");
		System.out.println("开始获取字节流....");
		int len=0,count=1,cc=1;
		System.out.print("总时进度:");
		for(int i=0;i<100;i++) {
			System.out.print("*");
		}
		System.out.print("\r\n任务进度:");
		boolean flag = true;
		byte[] bytes = new byte[2];
		while((len=bis.read(bytes))!=-1) {
			byte temp = bytes[0];
			bytes[0] = bytes[1];
			bytes[1] = temp;
			bos.write(bytes);
			count += 1;
			if(count%400000==0&&flag) {	//这里因为的我视频大小是82542592字节,然后我总任务打印了一百次*,
										//这里每次打印一个*,每次读取两个字节,所以用82542592/2/x = 100
										//x算出来等于41万左右,我就取了四十万,然后取四十万应该打印105次左右
										//因为后面的时间很短我就忽略掉了,取了一百次,后面就不循环这个了,
				System.out.print("*");
				cc += 1;
				if(cc==101) {
					flag = false;
					System.out.println("\r\n初始化....");
				}
			}
		}
		bis.close();
		bos.close();
	}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
好了看到这里,喜欢的话记得点赞收藏。有什么疑问在评论区给我提问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值