Channel 表示IO æºä¸ç®æ æå¼çè¿æ¥ãChannel 类似äºä¼ ç»çâæµâãåªä¸è¿Channel æ¬èº«ä¸è½ç´æ¥è®¿é®æ°æ®ï¼Channel åªè½ä¸Buffer è¿è¡äº¤äºãÂ
 ééç主è¦å®ç°ç±»
 java.nio.channels.Channel æ¥å£ï¼
 |--FileChannel
 |--SocketChannel
 |--ServerSocketChannel
 |--DatagramChannel
 Â
 è·åéé
 1. Java éå¯¹æ¯æééçç±»æä¾äº getChannel() æ¹æ³
 æ¬å° IOï¼
 FileInputStream/FileOutputStream
 RandomAccessFile
 Â
 ç½ç»IOï¼
 Socket
 ServerSocket
 DatagramSocket
Â
 2. å¨ JDK 1.7 ä¸ç NIO.2 é对å个ééæä¾äºéææ¹æ³ open()
 3. å¨ JDK 1.7 ä¸ç NIO.2 ç Files å·¥å ·ç±»ç newByteChannel()
 Â
 ééä¹é´çæ°æ®ä¼ è¾
 transferFrom()
 transferTo()
 Â
Â åæ£(Scatter)ä¸èé(Gather)
Â åæ£è¯»åï¼Scattering Readsï¼ï¼å°ééä¸çæ°æ®åæ£å°å¤ä¸ªç¼å²åºä¸
 èéåå ¥ï¼Gathering Writesï¼ï¼å°å¤ä¸ªç¼å²åºä¸çæ°æ®èéå°ééä¸
 Â
 å符éï¼Charset
 ç¼ç ï¼å符串 -> åèæ°ç»
 解ç ï¼åèæ°ç» Â -> å符串
ä¾åå¦ä¸
package com.buerc.nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;
public class TestChannel {
//å©ç¨éé宿æä»¶çå¤å¶ï¼éç´æ¥ç¼å²åºï¼
@Test
public void test1() throws IOException {
Instant start=Instant.now();
FileInputStream fis=new FileInputStream("G:\\èææº\\VMware-workstation_full_12.2.0.1269.exe");
FileOutputStream fos=new FileOutputStream("G:\\èææº\\VMware.exe");
FileChannel fc1=fis.getChannel();//为æä»¶å»ºç«éé
FileChannel fc2=fos.getChannel();//为æä»¶å»ºç«éé
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);//éç´æ¥ç¼å²åº
while((fc1.read(byteBuffer))!=-1) {//ä»ééè¯»åæ°æ®å°ç¼å²åº
byteBuffer.flip();//åæ¢ä¸ºè¯»æ¨¡å¼
fc2.write(byteBuffer);//å°ç¼å²åºæ°æ®åå
¥éé
byteBuffer.clear();
}
fc2.close();
fc1.close();
fos.close();
fis.close();
Instant end=Instant.now();
System.out.println(Duration.between(start, end));//PT6.565S
}
//使ç¨ç´æ¥ç¼å²åºå®ææä»¶çå¤å¶(å
åæ å°æä»¶)
@Test
public void test2() throws IOException {
Instant start=Instant.now();
FileChannel fc1=FileChannel.open(Paths.get("G:\\èææº\\VMware-workstation_full_12.2.0.1269.exe"), StandardOpenOption.READ);//建ç«ä¸ä¸ªå°æä»¶çéé
FileChannel fc2=FileChannel.open(Paths.get("G:\\èææº\\VMware.exe"), StandardOpenOption.WRITE,StandardOpenOption.READ);//建ç«ä¸ä¸ªå°æä»¶çéé
//å
åæ å°æä»¶
MappedByteBuffer mappedByteBuffer1=fc1.map(MapMode.READ_ONLY, 0, fc1.size());//å°éé对åºçæä»¶ç´æ¥æ å°å°å
å
MappedByteBuffer mappedByteBuffer2=fc2.map(MapMode.READ_WRITE, 0, fc1.size());
//ç´æ¥å¯¹ç¼å²åºä¸å½çæ°æ®æä½
byte[] dst=new byte[mappedByteBuffer1.limit()];
mappedByteBuffer1.get(dst);//ä»å
åæ å°æä»¶è·åæ°æ®å°æå®åèæ°ç»
mappedByteBuffer2.put(dst);//å°åèæ°åå
容åå
¥å
åæ å°æä»¶æå¯¹åºçééçæä»¶ä¸å»
fc2.close();
fc1.close();
Instant end=Instant.now();
System.out.println(Duration.between(start, end));//PT0.429S
}
//ééä¹é´çæ°æ®ä¼ è¾(ç´æ¥ç¼å²åº)
@Test
public void test3() throws IOException {
Instant start=Instant.now();
FileChannel fc1=FileChannel.open(Paths.get("G:\\èææº\\VMware-workstation_full_12.2.0.1269.exe"), StandardOpenOption.READ);
FileChannel fc2=FileChannel.open(Paths.get("G:\\èææº\\VMware.exe"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);
// fc1.transferTo(0, fc1.size(), fc2);//ç´æ¥å°éé1ä¸çæ°æ®ä¼ è¾å°éé2ä¸å¯¹åºçæä»¶
fc2.transferFrom(fc1, 0, fc1.size());
Instant end=Instant.now();
System.out.println(Duration.between(start, end));//PT0.429S
}
//忣åèé
@Test
public void test4() throws IOException {
RandomAccessFile ras=new RandomAccessFile("1.txt", "rw");
//è·åéé
FileChannel fc1=ras.getChannel();
ByteBuffer byteBuffer1=ByteBuffer.allocate(1024);
ByteBuffer byteBuffer2=ByteBuffer.allocate(1024);
ByteBuffer[] byteBuffers= {byteBuffer1,byteBuffer2};
//åæ£è¯»å
fc1.read(byteBuffers);
for (ByteBuffer byteBuffer : byteBuffers) {
byteBuffer.flip();
}
System.out.println(new String(byteBuffers[0].array()));
System.out.println("----------------------");
System.out.println(new String(byteBuffers[1].array()));
RandomAccessFile ras2=new RandomAccessFile("2.txt", "rw");
FileChannel fc2=ras2.getChannel();
//èéåå
¥
fc2.write(byteBuffers);
fc2.close();
ras2.close();
fc1.close();
ras.close();
}
@Test
public void test5() {
Map<String, Charset> map=Charset.availableCharsets();
Set<Entry<String, Charset>> set=map.entrySet();
for (Entry<String, Charset> entry : set) {
System.out.println(entry.getKey()+"\t"+entry.getValue());
}
}
@Test//å符é
public void test6() throws IOException {
Charset charset=Charset.forName("UTF-8");//æå®å符ç¼ç é
CharsetEncoder charsetEncoder=charset.newEncoder();//è·åç¼ç å¨
CharsetDecoder charsetDecoder=charset.newDecoder();//è·åè§£ç å¨
CharBuffer in=CharBuffer.allocate(1024);
in.put("helloworld");
in.flip();
ByteBuffer byteBuffer=charsetEncoder.encode(in);
for (int i = 0; i < in.limit(); i++) {
System.out.println(byteBuffer.get(i));
}
CharBuffer out=charsetDecoder.decode(byteBuffer);
for (int i = 0; i <out.limit(); i++) {
System.out.print(out.get());
}
}
}