チャネル(パイプ):
1.流れは似ていますが、相違点は次のとおりです。
チャネルが同時に流れのみ読み出しと書き込み、読み取り又は書き込みすることができる
通路が非同期書き込みデータであってもよい
(バッファ)、バッファにはまた、書き込みデータ、チャネルデータをバッファ(バッファ)から読み取ることができます
2.BIOストリームは書き込みもすることができます、あなたが読むことができる、両方向に、チャンネル一方向であります
3チャンネルは、NIOのインターフェイスであります
FileChannel、のDatagramChannel、ServerScoketChannelとのSocketChannel:チャンネルカテゴリは一般的に使用されている
ServerScoketChannelは、データを読み書きするために、データ用のFileChannelデータの読み取りと書き込みのファイル、UDPののDatagramChannelは、読み取りおよび書き込み、およびTCPのSocketChannel
NIOのネットワーク通信プロセス
これは、クライアントに対応して生成たSocketChannel(SocketChannelImpl)を生成する、サーバ側のServerScoketChannel(ServerScoketChannelImpl)を作成します
主な方法をチャンネル:
1. 読む:データはチャンネルから読み出され、バッファに置か
2. 書き込み:バッファにデータチャネルを書く
3. transferFrom:現在のチャネルに先チャネルからのコピーデータ
4 transferTo現在のデータ:先のチャンネルにコピーされたチャネル
のファイルの読み込みにチャネル書き込みや書き込み操作、次
のファイルへの書き込み
package com.jym.nio;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @program: JymNetty
* @description: Channel学习
* @author: jym
* @create: 2020/02/01
*/
public class JymFileChannel {
public static void main(String[] args) throws Exception {
String str = "hello,jym";
FileOutputStream fileOutputStream = new FileOutputStream("D:\\jym.txt");
// 通过fileOutputStream获取对应的channel,真实类型为FileChannelImpl
FileChannel fileChannel = fileOutputStream.getChannel();
// 创建一个缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// 将str放入到byteBuffer
byteBuffer.put(str.getBytes());
// 将byteBuffer进行翻转,因为需要读
byteBuffer.flip();
// 将byteBuffer数据写入到fileChannel
fileChannel.write(byteBuffer);
fileOutputStream.close();
}
}
ファイルの内容を読みます:
package com.jym.nio;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @program: JymNetty
* @description: 读取数据
* @author: jym
* @create: 2020/02/01
*/
public class JymFileChannel1 {
public static void main(String[] args) throws Exception {
File file = new File("D:\\jym.txt");
FileInputStream fileInputStream = new FileInputStream(file);
// 通过fileInputStream 获取channel,实际类型为FileChannelImpl
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
// 将通道数据读入到缓冲区中
fileInputStreamChannel.read(byteBuffer);
// 将字节信息转成字符串
System.out.println(new String(byteBuffer.array()));
fileInputStream.close();
}
}
私たちのすべて一緒に、ファイルのコピーが完了したことをしてみましょう
package com.jym.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @program: JymNetty
* @description: 文件拷贝练习
* @author: jym
* @create: 2020/02/01
*/
public class JymFileChannel2 {
public static void main(String[] args) throws Exception {
File file = new File("D:\\壁纸\\1.jpg");
FileInputStream fileInputStream = new FileInputStream(file);
// 通过fileInputStream 获取channel,实际类型为FileChannelImpl
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("3.jpg");
// 通过fileOutputStream获取对应的channel,真实类型为FileChannelImpl
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(512);
while (true){
byteBuffer.clear();
// 将通道数据读入到缓冲区中
int read = fileInputStreamChannel.read(byteBuffer);
if(read == -1){
break;
}
// 需要写,所以得切换
byteBuffer.flip();
// 将byteBuffer数据写入到fileChannel
fileOutputStreamChannel.write(byteBuffer);
}
fileInputStream.close();
fileOutputStream.close();
}
}
注:クリーンする必要が()は、クリーンでない場合、それは無限ループに、0として読まれている場合、ループ内のデータを読み出します
ファイルをコピーする方法を転送し、このプロセスは簡単たくさんあります:
package com.jym.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
/**
* @program: JymNetty
* @description: transfer测试
* @author: jym
* @create: 2020/02/01
*/
public class JymFileChannel3 {
public static void main(String[] args) throws Exception {
File file = new File("D:\\壁纸\\1.jpg");
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("4.jpg");
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
fileOutputStreamChannel.transferFrom(fileInputStreamChannel,0,fileInputStreamChannel.size());
fileInputStreamChannel.close();
fileOutputStreamChannel.close();
fileInputStream.close();
fileOutputStream.close();
}
}