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

不点 阅读:211 2021-03-31 21:13:09 评论:0


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


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们