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();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Handsome2013/article/details/85934245