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;
	}
发布了45 篇原创文章 · 获赞 14 · 访问量 2476

猜你喜欢

转载自blog.csdn.net/qq_44357371/article/details/103175196