基于Java NIO 实现文件基础功能:写入、读取和拷贝功能(基础代码)



package com.springcloud.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class NIOFileUtil {
	
	private String file;
	
	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}

	public NIOFileUtil(String file) throws IOException {
		super();
		this.file = file;
	}
	
	public void read(int allocate) throws IOException{
		//RandomAccessFile access = new RandomAccessFile(this.file, "r");
		FileInputStream inputStream = new FileInputStream(this.file);
		FileChannel channel = inputStream.getChannel();
		ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
		// 解决汉字乱码问题
		CharBuffer charBuffer = CharBuffer.allocate(allocate);
		Charset charset = Charset.forName("GBK");
        CharsetDecoder decoder = charset.newDecoder();
        
		int length = channel.read(byteBuffer);
		while(length != -1){
			byteBuffer.flip();			
            decoder.decode(byteBuffer, charBuffer, true);
            charBuffer.flip();
            System.out.println(charBuffer.toString());
            // 清空缓存
            byteBuffer.clear();
            charBuffer.clear();
            // 再次读取文本内容
            length = channel.read(byteBuffer);
            
		}
		channel.close();
		if(inputStream != null){
			inputStream.close();
		}		
	}
	
	public void write(String context, int allocate, String chartName) throws IOException{
		// FileOutputStream outputStream = new FileOutputStream(this.file); //文件内容覆盖模式 --不推荐
		FileOutputStream outputStream = new FileOutputStream(this.file, true); //文件内容追加模式--推荐
		FileChannel channel = outputStream.getChannel();
		ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
		byteBuffer.put(context.getBytes(chartName));
		byteBuffer.flip();//读取模式转换为写入模式
		channel.write(byteBuffer);
		channel.close();
		if(outputStream != null){
			outputStream.close();
		}
	}
	
	public void cpoy(String source, String target, int allocate) throws IOException{
		ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
		
		FileInputStream inputStream = new FileInputStream(source);
		FileChannel inChannel = inputStream.getChannel();
		
		FileOutputStream outputStream = new FileOutputStream(target);
		FileChannel outChannel = outputStream.getChannel();
		
		int length = inChannel.read(byteBuffer);
		while(length != -1){
			byteBuffer.flip();//读取模式转换写入模式
			outChannel.write(byteBuffer);
			byteBuffer.clear(); //清空缓存,等待下次写入
			// 再次读取文本内容
            length = inChannel.read(byteBuffer);
		}
		outputStream.close();
		outChannel.close();
		inputStream.close();
		inChannel.close();
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		NIOFileUtil util = new NIOFileUtil("D://内容.txt");
		//util.read(20);
//		String context ="我爱我的祖国123456";
//		util.write(context, 40, "GBK");
		
		util.cpoy("D://内容.txt", "D://赋值内容.txt", 1024);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值