1. 程式人生 > >Java NIO知識整理

Java NIO知識整理

前幾天學了下NIO這塊,因之前基本沒用到過也算是新知識,這篇文章著重分享Channel,記憶體對映,緩衝區不會做過多介紹,有興趣可以百度一下找資料看

一   使用通道邊讀邊寫的經典寫法

以複製圖片到同一個目錄為例,把wp.jpg複製一份放到d盤下

package com.debug;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class UseChanel01 {

	public static void main(String[] args) throws Exception {
	
        File f1=new File("d:"+File.separator+"wp.jpg");
        File f2=new File("d:"+File.separator+"wp01.jpg");
        FileInputStream in=new FileInputStream(f1);
        FileOutputStream out=new FileOutputStream(f2);
        
        //取得輸入輸出流的通道
        FileChannel inChanel=in.getChannel();
        FileChannel outChanel=out.getChannel();
        //開闢緩衝
        ByteBuffer buf=ByteBuffer.allocate(1024);
        
        while(inChanel.read(buf)!=-1) {
        	buf.flip();//重設緩衝區
        	outChanel.write(buf);//輸出到緩衝區
        	buf.clear();//清空緩衝區
        	
        }
        outChanel.close();
        inChanel.close();
        out.close();
        in.close();
        
        
	}

}

二  NIO2的寫法

NIO2之後簡化了獲取Channel的方法,不需要通過InputStream和OutoutStream獲取,程式碼如下

package com.debug;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class UseChannel02 {

	public static void main(String[] args) throws Exception {
		//NIO2 取得channel(不需要通過輸入輸出流取得)
		FileChannel inChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp.jpg"), StandardOpenOption.READ);
		FileChannel outChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp03.jpg"),StandardOpenOption.READ, StandardOpenOption.WRITE,StandardOpenOption.CREATE);

		//使用記憶體對映的方式'邊讀邊寫'
		MappedByteBuffer mapByteInBuffer=inChaeenl.map(MapMode.READ_ONLY, 0, inChaeenl.size());
		MappedByteBuffer  mapByteOutBuffer=outChaeenl.map(MapMode.READ_WRITE, 0, inChaeenl.size());
		
		byte[] dst=new byte[mapByteInBuffer.limit()];
		mapByteInBuffer.get(dst);
		mapByteOutBuffer.put(dst);
		
		outChaeenl.close();
		inChaeenl.close();
		
	}

}

三  除使用記憶體對映的方式還有其他使用起來更簡潔的API
package com.debug;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class UseChannel03 {

	public static void main(String[] args) throws Exception {
		
		//NIO2 取得channel
		FileChannel inChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp.jpg"), StandardOpenOption.READ);
		FileChannel outChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp04.jpg"),StandardOpenOption.READ, StandardOpenOption.WRITE,StandardOpenOption.CREATE);

		//使用transferFrom或者transferTo進行邊讀邊寫(最簡潔的方式)
		outChaeenl.transferFrom(inChaeenl, 0, inChaeenl.size());
		
		outChaeenl.close();
		inChaeenl.close();
		
	}

}