题目描述:
限定使用socket通信来完成。需要实现的功能是:客户端读取10G的文本
文件以"换行啦啦啦"作为换行符、将文件中的内容、按行发送给服务端。服务端接收数据后、将文本文件保存在服务端所在电脑的磁盘上。合理使用线程!!
限定使用Socket通信,内存4G去传输10G的文本。并且读取过程需要换行,应该如何实现?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答
- ꧁gaoKuo꧂ 2019-04-25 11:32关注
public static void main(String[] args) throws IOException { //监听端口 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //切换非阻塞 serverSocketChannel.configureBlocking(false); //绑定端口 serverSocketChannel.bind(new InetSocketAddress("localhost",9696)); //注册选择器 Selector selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); FileChannel outChannel = FileChannel.open(Paths.get("你的文件"), StandardOpenOption.WRITE, StandardOpenOption.CREATE); while (selector.select() > 0){ Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while(iterator.hasNext()){ SelectionKey selectionKey = iterator.next(); if(selectionKey.isAcceptable()){ SocketChannel client = serverSocketChannel.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); }else if(selectionKey.isReadable()){ SocketChannel client = (SocketChannel) selectionKey.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (client.read(buffer) > 0){ buffer.flip(); //byte[] bytes = new byte[buffer.limit()]; //buffer.get(bytes); outChannel.write(buffer); } } iterator.remove(); } } }
换行符自个替换
解决 无用评论 打赏 举报
悬赏问题
- ¥20 Python安装cvxpy库出问题
- ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
- ¥15 python天天向上类似问题,但没有清零
- ¥30 3天&7天&&15天&销量如何统计同一行
- ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
- ¥15 C#调用python代码(python带有库)
- ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
- ¥15 活动选择题。最多可以参加几个项目?
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题