【java】nio读写

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import org.junit.Test;

public class NioMain {
	@Test
	public  void bioread(){
		InputStream in = null;
		try{
			in = new BufferedInputStream(new FileInputStream("src/nomal_io.txt"));
			
			InputStreamReader inputStreamReader = new InputStreamReader(in,"UTF-8"); 
			BufferedReader br = new BufferedReader(inputStreamReader); 
			String data = null;
			while((data = br.readLine())!=null){
				System.out.println(data); 
			}
			
//			byte [] buf = new byte[1024];
//			int bytesRead = in.read(buf);
//			while(bytesRead != -1){
//				for(int i=0;i<bytesRead;i++){
//					System.out.print((char)buf[i]);
//				}
//				bytesRead = in.read(buf);	
//			}
		}catch (IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(in != null){
					in.close();
				}
			}catch (IOException e){
				e.printStackTrace();
			}
		}
	}
	
	

	@Test
	public  void readByte(){
		//数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中

		RandomAccessFile aFile = null;
		try{

			aFile = new RandomAccessFile("src/nio.txt","rw");
			FileChannel fileChannel = aFile.getChannel();

			ByteBuffer buf = ByteBuffer.allocate(1024);
			int bytesRead = fileChannel.read(buf);

			while(bytesRead != -1){
				buf.flip();
				while(buf.hasRemaining()){
					System.out.print((char)buf.get());
				}

				buf.compact();
				bytesRead = fileChannel.read(buf);
			}
			
		}catch (IOException e){
			e.printStackTrace();
		}
		finally{
			try{
				if(aFile != null){
					aFile.close();
				}
			}catch (IOException e){
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 */
	@Test
	public void readStr(){
		RandomAccessFile raf =null;
		try {  
			Charset charset = Charset.forName("UTF-8");
			CharsetDecoder decoder = charset.newDecoder();  

			raf = new RandomAccessFile("src/nio.txt", "rw");  
			FileChannel fc = raf.getChannel();  

			ByteBuffer buffer = ByteBuffer.allocate(512);   
			CharBuffer cb = CharBuffer.allocate(512);  

			int count = fc.read(buffer);  
			while (count != -1) {   
				buffer.flip();  
				decoder.decode(buffer, cb, false);  
				cb.flip();  
				while (cb.hasRemaining()) {  
					System.out.print(cb.get());  
				}   
				buffer.clear();  
				cb.clear();  
				count = fc.read(buffer);  
			}  
		} catch (Exception e) {  
			e.printStackTrace();  
		}
		finally{
			try{
				if(raf != null){
					raf.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		
		
		
	}
	
	/**
	 * nio 拷贝文件
	 */
	@Test
	public  void copyfile(){
		RandomAccessFile fromFile = null;
		RandomAccessFile toFile = null;
		try{
			fromFile = new RandomAccessFile("src/fromFile.txt","rw");
			FileChannel fromChannel = fromFile.getChannel();
			toFile = new RandomAccessFile("src/toFile.txt","rw");
			FileChannel toChannel = toFile.getChannel();

			fromChannel.transferTo(0,fromChannel.size(),toChannel);

		}catch (IOException e){
			e.printStackTrace();
		}
		finally{
			try{
				if(fromFile != null){
					fromFile.close();
				}
				if(toFile != null){
					toFile.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	
}

转载于:https://my.oschina.net/v512345/blog/1530080

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值