Java NIO FileChannel读写复制文件

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileTest {
	public static void main(String[] args) {
		try {
			new FileTest();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public FileTest() throws Exception {
		String pathIn = "D:/code/file.txt";
		String pathOut = "D:/code/copy.txt";

		FileChannel fcIn = new RandomAccessFile(pathIn, "rw").getChannel();
		System.out.println("源文件总长度:" + fcIn.size());

		System.out.println("源文件全部内容:");
		printer(pathIn);

		FileChannel fcOut = new FileOutputStream(pathOut).getChannel();

		final int capacity = 4;
		ByteBuffer buffer = ByteBuffer.allocate(capacity);

		int count = 0;
		while (true) {
			count = fcIn.read(buffer);
			if (count == -1) {
				break;
			}

			buffer.flip();
			fcOut.write(buffer);
			buffer.clear();
		}

		System.out.println("复制文件总长度:" + fcOut.size());
		System.out.println("复制文件全部内容:");
		printer(pathOut);
	}

	// 打印path指向的文件中的内容。
	private void printer(String path) throws Exception {
		InputStream is = new FileInputStream(path);
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String s = null;
		while (true) {
			s = br.readLine();
			if (s != null)
				System.out.println(s);
			else
				break;
		}

		br.close();
		is.close();
	}
}

运行后输出:

源文件总长度:10
源文件全部内容:
0123456789
复制文件总长度:10
复制文件全部内容:
0123456789

 

上面是常规的文件复制,包含对源文件的读,以及把读出来的文件写入到目标复制文件。在Java NIO中,用FileChannel复制文件有更简单的方案,一行代码实现,如下:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class FileTest {
	public static void main(String[] args) {
		try {
			new FileTest();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public FileTest() throws Exception {
		String pathIn = "D:/code/file.txt";
		String pathOut = "D:/code/copy.txt";

		FileChannel fcIn = new RandomAccessFile(pathIn, "rw").getChannel();
		System.out.println("源文件总长度:" + fcIn.size());

		System.out.println("源文件全部内容:");
		printer(pathIn);

		FileChannel fcOut = new FileOutputStream(pathOut).getChannel();

		// FileChannel方式的复制,一行代码实现。
		fcIn.transferTo(0, fcIn.size(), fcOut);

		System.out.println("复制文件总长度:" + fcOut.size());
		System.out.println("复制文件全部内容:");
		printer(pathOut);
	}

	// 打印path指向的文件中的内容。
	private void printer(String path) throws Exception {
		InputStream is = new FileInputStream(path);
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String s = null;
		while (true) {
			s = br.readLine();
			if (s != null)
				System.out.println(s);
			else
				break;
		}

		br.close();
		is.close();
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值