Java文件读写(一)

这篇博客介绍怎样利用Java的读写功能实现

  1. 文件复制
  2. 文件加密
  3. 提升读写性能
一、文件的读

Java中读取和写入文件都是通过流来进行的
在这里插入图片描述
图片来源:https://blog.csdn.net/zhangbinu/article/details/51362612/
我们这篇文章只用按字符读取

public String read(String fileName){	//filename是文件的地址
		String str = null;
		try {
			FileInputStream fis = new FileInputStream(fileName);	//创建一个流
			int length = fis.available();	
	//这里available方法返回可从此输入流读取(或跳过)的剩余字节数的估计值,可理解为文件剩余要读的大小
			//创建字节数组,用于存放读取的字节
			byte[] bytes = new byte[length];
			int b=0;
			while(b!=-1){
			//读的存入bytes,返回值含义:当读到文件末尾,返回-1
				b=fis.read(bytes);
			}
			str = new String(bytes);
			fis.close();	//每次读取要关闭流
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
二、文件复制

同样利用流对文件进行操作

public void write(String Infile,String Outfile){
//我这个write方法,Infile代表需要复制的文件,Outfile代表输出文件(复制文件)
		byte[] bytes;
		try {
			FileInputStream fis = new FileInputStream(Infile);
			FileOutputStream fos = new FileOutputStream(Outfile);
			int length = fis.available();
			//读取一个字节
			bytes = new byte[length];
			int b=0;
			while(b!=-1){
				b=fis.read(bytes);
				fos.write(b);
			}
			fis.close();
			fos.flush();	//保证稳定性
			fos.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
三、文件的简单加密

我们可以对复制的文件进行加密,因为文件是按照一个个字节来复制的,每个字节对应着一个ASCII码,那么我们在写入的时候,只需把写入的字节改掉就行了
例如:在while循环中,将字节加一,那么写入的结果就改变了

public void jiamiWrite(String Infile,String Outfile){
		try {
			FileInputStream fis = new FileInputStream(Infile);
			FileOutputStream fos = new FileOutputStream(Outfile);
			int length = fis.available();
			//读取一个字节
			byte[] bytes = new byte[length];
			int b=fis.read(bytes);
			while(b!=-1){
				b=fis.read(bytes);
				bytes = jiami(bytes);	//在这里是对要写入的bytes进行改变
				fos.write(bytes);
			}
			fis.close();
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
public byte[] jiami(byte[] bytes){
		for(int i =0;i<bytes.length;i++){
			bytes[i]++;
		}
		return bytes;
	}
四、文件解密

文件的解密思路和加密思路一样,既然知道了加密文件是因为b++导致的,那么解密的时候只用把b–就好了

	public String jiemiRead(String fileName){
		String str = null;
		try {
			FileInputStream fis = new FileInputStream(fileName);
			java.io.BufferedInputStream bis = new BufferedInputStream(fis);
			int length = bis.available();
			//读取一个字节
			byte[] bytes = new byte[length];
			int b=0;
			while(b!=-1){
				bytes = jiemi(bytes);
				b=fis.read(bytes);
			}
			str = new String(bytes);
			System.out.println(str);
			bis.close();
			fis.close();
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
public byte[] jiemi(byte[] bytes){
		for(int i=0;i<bytes.length;i++){
			bytes[i]--;
		}
		return bytes;
	}
五、提升读写性能

我们之前已经知道,Java画线程图的时候,经常会用到buffer,在流中,同样也有buffer,在使用时,只需要把buffer嵌套进去即可

public String bufferRead(String fileName){
		String str = null;
		try {
			FileInputStream fis = new FileInputStream(fileName);
			//相当于在fis上面多加了一层装备
			java.io.BufferedInputStream bis = new BufferedInputStream(fis);
			int length = bis.available();
			//读取一个字节
			byte[] bytes = new byte[length];
			int b=0;
			while(b!=-1){
				b=fis.read(bytes);
			}
			str = new String(bytes);
			bis.close();
			fis.close();
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值