1.代码
package com.ljf.netty.nio;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class WriteToLocalDemo {
public static void main(String[] args) throws IOException {
String str="hello,中国";
String dstPath="d:/liu.txt";
//创建一个输出流
FileOutputStream fileOutputStream=new FileOutputStream(dstPath);
//通过fileOutputStream获取对应的FileChannel
FileChannel fileChannel=fileOutputStream.getChannel();
//创建一个缓冲区 ByteBuffer
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
//将str内容放到缓存中
byteBuffer.put(str.getBytes());
//对bypteBuffer进行flip
byteBuffer.flip();
//将byteBuffer数据写入到fileChannel中
fileChannel.write(byteBuffer);
fileChannel.close();
}
}
2.效果
1.2 读取本地文件进行输出1.代码
package com.ljf.netty.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadFromLocalDemo {
public static void main(String[] args) throws IOException {
File file=new File("d:/liu.txt");
//创建文件的输入流
FileInputStream fileInputStream=new FileInputStream(file);
//通过fileinputstream 获取对应的filechannel
FileChannel fileChannel=fileInputStream.getChannel();
//创建缓存区
ByteBuffer byteBuffer=ByteBuffer.allocate((int)file.length());
//将通道的数据读入到Buffer
fileChannel.read(byteBuffer);
System.out.println(new String(byteBuffer.array()));
fileChannel.close();
}
}
2.结果
1.3 实现文件的复制操作1.代码
package com.ljf.netty.nio;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannel01 {
public static void main(String[] args) throws IOException {
//读
FileInputStream fileInputStream=new FileInputStream("d:/liu.txt");
FileChannel fileChannel01=fileInputStream.getChannel();
//写
FileOutputStream fileOutputStream=new FileOutputStream("d:/liu2.txt");
FileChannel fileChanne02=fileOutputStream.getChannel();
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
//循环
while(true){
//清空buffer,防止上一次存储的数据存储,
byteBuffer.clear();
int read= fileChannel01.read(byteBuffer);
if(read==-1){
System.out.println("已经读完,退出");
break;
}
//将buffer中的数据写入到filechannel02中
byteBuffer.flip();
fileChanne02.write(byteBuffer);
}
fileChannel01.close();
fileChanne02.close();
System.out.println("结束......");
}
}
2.结果
1.4 使用transferFrom实现文件的复制操作1.代码
package com.ljf.netty.nio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class TransferFileDemo {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=new FileInputStream("d:/liu.txt");
FileOutputStream fileOutputStream=new FileOutputStream("d:/liu3.txt");
//获取各个流对应的filechannel
FileChannel sourceCh=fileInputStream.getChannel();
FileChannel destCh=fileOutputStream.getChannel();
//使用transferFrom完成复制
destCh.transferFrom(sourceCh,0,sourceCh.size());
//关闭通道
sourceCh.close();
destCh.close();
fileInputStream.close();
fileOutputStream.close();
}
}
2.结果
1.5 通过使用多个buffer完成读写操作//Scattering:将数据写入到buffer时,可以采用buffer数组,依次写入【分散】 //Gathering: 从buffer读取数据时,可以采用buffer数组,依次读
1.代码
package com.ljf.netty.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class BacthCopy {
public static void main(String[] args) throws IOException {
//Scattering:将数据写入到buffer时,可以采用buffer数组,依次写入【分散】
//Gathering: 从buffer读取数据时,可以采用buffer数组,依次读
//使用 ServerSocketChannel和socketChannel网络
ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
InetSocketAddress inetSocketAddress=new InetSocketAddress(7000);
//绑定端口到socket,并启动
serverSocketChannel.socket().bind(inetSocketAddress);
//创建buffer数组
ByteBuffer[] byteBuffers=new ByteBuffer[2];
byteBuffers[0]=ByteBuffer.allocate(5);
byteBuffers[1]=ByteBuffer.allocate(3);
//等客户端连接
SocketChannel socketChannel=serverSocketChannel.accept();
int messageLenght=8;
//循环读取
while(true){
int byteRead=0;
while(byteRead "位置:"+bb.position()+" limit:"+bb.limit()).forEach(System.out::println);
List dataList=Arrays.asList(byteBuffers);
}
//将所有的buffer进行flip
Arrays.asList(byteBuffers).forEach(buffer->buffer.flip());
//将数据读出,显示到客户端
long byteWirte=0;
while(byteWirte{buffer.clear();});
System.out.println("byteRead:"+byteRead+" bytewrite:"+byteWirte+"messagelength:"+messageLenght);
}
}
}
2.测试结果
cmd窗口,输入: telnet localhost 7000
按住,ctr+] 进行发送数据:
send zhongguo nihao



