java.nio.* 篇(1) FileChannel AsynchronousFileChannel ServerSocket Socket 使用案例
2023-03-14 22:44:06 时间
java.nio.FileChannel 使用demo
/** * @description: demos of jdk8 java.nio.FileChannel class * java.nio.FileChannel 作用:FileChannel 是文件通道类, 管道形式打开文件 * FileChannel MappedByteBuffer 例子: */ @Test public void testFileChannel() throws IOException { //filechannel 写入demo byte[] bytes = "hello world".getBytes(); RandomAccessFile fis = new RandomAccessFile("D:\soft\doc\file_channel.txt","rw"); FileChannel fileChannel = fis.getChannel(); MappedByteBuffer writembb = fileChannel.map(FileChannel.MapMode.READ_WRITE,0,1024*1024); writembb.put(bytes,0,bytes.length); fis.close(); fileChannel.close(); //filechannel 读取demo byte[] rbytes = new byte[1024]; fis = new RandomAccessFile("D:\soft\doc\file_channel.txt","rw"); fileChannel = fis.getChannel(); MappedByteBuffer readmbb = fileChannel.map(FileChannel.MapMode.READ_WRITE,0,1024*1024); readmbb.position(0); readmbb.get(rbytes,0,bytes.length); String rb = new String(rbytes); System.out.println(rb); }
java.nio.AsynchronousFileChannel 使用demo
/** * @description: demos of jdk8 java.nio.AsynchronousFileChannel class * java.nio.AsynchronousFileChannel 作用:AsynchronousFileChannel 是 异步文件通道类, 管道形式打开文件 * FileChannel MappedByteBuffer 例子: */ @Test public void testAsynchronousFileChannel() throws IOException { //asynchronousFileChannel 写入demo ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byteBuffer.put("hello".getBytes()); AsynchronousFileChannel wfileChannel = AsynchronousFileChannel.open(Paths.get("D:/soft/doc/file_channel2.txt"), StandardOpenOption.CREATE,StandardOpenOption.WRITE); byteBuffer.flip(); wfileChannel.write(byteBuffer, 0, null, new CompletionHandler<Integer, Object>() { @Override public void completed(Integer result, Object attachment) { try { wfileChannel.force(true); wfileChannel.close(); }catch (Exception e){ e.printStackTrace(); } } @Override public void failed(Throwable exc, Object attachment) { } }); //asynchronousFileChannel 读取demo byteBuffer.clear(); AsynchronousFileChannel rFileChannel = AsynchronousFileChannel.open(Paths.get("D:/soft/doc/file_channel2.txt"), StandardOpenOption.READ); rFileChannel.read(byteBuffer, 0, null, new CompletionHandler<Integer, Object>() { @Override public void completed(Integer result, Object attachment) { try { byteBuffer.flip(); System.out.println(new String(byteBuffer.array())); rFileChannel.close(); }catch (Exception e){ e.printStackTrace(); } } @Override public void failed(Throwable exc, Object attachment) { } }); }
java.net.ServerSocket 使用demo
/** * @description: demos of jdk8 java.net.ServerSocket class * 创建服务端,有客户端连接后发送hello 字符串,并接受客户端reply 回复 * * */ @Test public void testServer() throws IOException { ServerSocket serverSocket = new ServerSocket(8679); while(true){ Socket socket = serverSocket.accept(); socket.getOutputStream().write("hello".getBytes()); try{ ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byte[] bytes = new byte[1024]; int read = 0; while ((read =socket.getInputStream().read(bytes))>0){ byteBuffer.put(bytes,0,read); byteBuffer.flip(); String rs = new String(byteBuffer.array()); System.out.println(rs); byteBuffer.clear(); } }catch (Exception e){ e.printStackTrace(); }finally { if(!socket.isClosed()) { try { socket.close(); }catch (Exception e1){ e1.printStackTrace(); } } } } }
java.net.Socket 使用demo
/** * @description: demos of jdk8 java.net.Socket class * 创建客户端,连接到服务端后接收到hello 字符串,并接向服务端发送reply 字符串回复 */ @Test public void testClient() throws IOException { Socket socket = new Socket("127.0.0.1", 8679); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byte[] bytes = new byte[1024]; int read = 0; while ((read = socket.getInputStream().read(bytes)) > 0) { byteBuffer.put(bytes, 0, read); byteBuffer.flip(); String rs = new String(byteBuffer.array()); byteBuffer.clear(); System.out.println(rs); socket.getOutputStream().write("reply".getBytes()); } }
BIO 参考《通俗易懂的JAVA BIO NIO AIO 原理白话文解释,区别,优缺点及代码使用案例》
相关文章
- 在 Go 里用 CGO?这 7 个问题你要关注!
- 9款优秀的去中心化通讯软件 Matrix 的客户端
- 求职数据分析,项目经验该怎么写
- 在OKR中,我看到了数据驱动业务的未来
- 火山引擎云原生大数据在金融行业的实践
- OpenHarmony富设备移植指南(二)—从postmarketOS获取移植资源
- 《数据成熟度指数》报告:64%的企业领袖认为大多数员工“不懂数据”
- OpenHarmony 小型系统兼容性测试指南
- 肯睿中国(Cloudera):2023年企业数字战略三大趋势预测
- 适用于 Linux 的十大命令行游戏
- GNOME 截图工具的新旧截图方式
- System76 即将推出的 COSMIC 桌面正在酝酿大变化
- 2GB 内存 8GB 存储即可流畅运行,Windows 11 极致精简版系统 Tiny11 发布
- 迎接 ecode:一个即将推出的具有全新图形用户界面框架的现代、轻量级代码编辑器
- loongarch架构介绍(三)—地址翻译
- Go 语言怎么解决编译器错误“err is shadowed during return”?
- 敏捷:可能被开发人员遗忘的部分
- Denodo预测2023年数据管理和分析的未来
- 利用数据推动可持续发展
- 在 Vue3 中实现 React 原生 Hooks(useState、useEffect),深入理解 React Hooks 的