泰安市网站建设_网站建设公司_测试工程师_seo优化
2025/12/27 23:58:22 网站建设 项目流程

可以利用换行符进行切割

创建bytebuffer进行存储切割后的片段

@Slf4j public class ByteBuffer2Test { public static void main(String[] args) { ByteBuffer allocate = ByteBuffer.allocate(32); allocate.put("hello world\nheelo nihao\nhow".getBytes()); split(allocate); allocate.put(" are you\n".getBytes()); split(allocate); } public static void split(ByteBuffer byteBuffer) { byteBuffer.flip(); for (int i = 0; i < byteBuffer.limit(); i++) { byte b = byteBuffer.get(i); if (b == '\n') { int length = (i + 1) - byteBuffer.position(); ByteBuffer lineBuffer = ByteBuffer.allocate(length); for (int j = 0; j < length; j++) { lineBuffer.put(byteBuffer.get()); } lineBuffer.flip(); while (lineBuffer.hasRemaining()) { System.out.print((char) lineBuffer.get()); } } } byteBuffer.compact(); } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询