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;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章