1. 程式人生 > >java中怎麼將一個檔案內容寫入到另一個檔案,這裡分別使用io和nio來實現

java中怎麼將一個檔案內容寫入到另一個檔案,這裡分別使用io和nio來實現

一:使用io來實現將一個檔案內容寫入到另一個檔案

需要兩個檔案流,檔案輸入流和檔案輸出流,中間還需要一個作為記憶體中的輔助空間(類似於裝東西的小車),我們可以使用一個位元組陣列。

迴圈使用小車,從倉庫(源頭)運貨到商場(目的地)。

package aboutIO;

import java.io.*;

public class FileIO3 {
	public static void main(String[] args) {
		try {
			FileInputStream in=new FileInputStream("C://Users//ASUS//Desktop//a.txt");
			FileOutputStream out=new FileOutputStream("C://Users//ASUS//Desktop//c.txt");
			//建立一個小卡車,迴圈來回轉載貨物,從倉庫(源頭)到達商場(目的地)
			byte[]buffer=new byte[1024];
			int readLength;
			while((readLength=in.read(buffer))>0){//這裡的in.read(buffer);就是把輸入流中的東西,寫入到記憶體中(buffer)。
				System.out.println(new String(buffer,0,readLength));//這裡順利將位元組陣列轉化為了字串
				out.write(buffer);//這裡就是把記憶體中(buffer)的內容寫出到輸出流中,也就寫出到了指定檔案中
				
			}
			
		} catch ( Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

結果如下:

同時控制檯列印:

二:java中怎麼將一個檔案內容寫入到另一個檔案,這裡使用nio來實現

 

通道和緩衝器:通道是裝滿東西的房間,緩衝區就是一個卡車,到了目的地,在從緩衝區取下來
通道是目的地或者源頭,緩衝器就是中間的
位元組緩衝區類,就是唯一的直接和通道接通的緩衝區類
ByteBuffer是一個比較基礎的類

方式一:出了兩個通道,一個是目的地通道(channel2),一個是源頭的通道(channel1),中間還需要一個大卡車(ByteBuffer)

package aboutIO;

import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileIO {
	public static void main(String[] args) {
		try {
			FileInputStream file1 = new FileInputStream("C://Users//ASUS//Desktop//a.txt");
			FileChannel channel = file1.getChannel();
			FileOutputStream file2 = new FileOutputStream("C://Users//ASUS//Desktop//b.txt");
			FileChannel channel2 = file2.getChannel();
			// 下邊開始宣告一個位元組緩衝器
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			while (channel.read(buffer) != -1) {//將物品從庫存讀入到緩衝器(大卡車)
				buffer.flip();
				channel2.write(buffer);//將緩衝器(大卡車)的物品,寫出到目的地。
				buffer.clear();

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

//經過上邊操作,就將我桌面文件中的a.txt中內容,複製到了我們的桌面的b.txt

方式二:直接有通向兩個地方的一個方法,卡車都省了。直接從一個地方丟到另一個地方:

package aboutIO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileIO2 {
	public static void main(String[] args) {
		try {
			FileInputStream file1 = new FileInputStream("C://Users//ASUS//Desktop//a.txt");
			FileChannel channel = file1.getChannel();
			FileOutputStream file2 = new FileOutputStream("C://Users//ASUS//Desktop//b.txt");
			FileChannel channel2 = file2.getChannel();
			// //下邊開始宣告一個位元組緩衝器
			// ByteBuffer buffer=ByteBuffer.allocate(1024);
			// while(channel.read(buffer)!=-1){
			// buffer.flip();
			// channel2.write(buffer);
			// buffer.clear();
			// 使用下邊一句來代替上邊註釋部分的作用
			channel.transferTo(0, channel.size(), channel2);//也可以使用transferFrom方法,引數需要修改即可

		} catch (

		IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}