자바에 관한 책을 읽었으며 채널에서 ByteBuffer로 읽는 중입니다.누군가 왜 이런 식으로 while 루프를 구성합니까?
try (FileChannel inCh = (FileChannel) Files.newByteChannel(file)) { ByteBuffer lengthBuf = ByteBuffer.allocate(8); int strLength = 0; ByteBuffer[] buffers = { null, ByteBuffer.allocate(8) }; while(true) { if(inCh.read(lengthBuf) == -1) break; lengthBuf.flip(); strLength = (int)lengthBuf.getDouble(); buffers[0] = ByteBuffer.allocate(2*strLength); if(inCh.read(buffers) == -1) { System.err.println("EOF found reading ht eprime string."); break; } System.out.printf("String length: %3s String: %-12s Binary Value: %3d%n", strLength, ((ByteBuffer) (buffers[0].flip())).asCharBuffer().toString(), ((ByteBuffer)buffers[1].flip()).getLong()); lengthBuf.clear(); buffers[1].clear(); } System.out.println("\nEOF reached."); } catch (IOException e) {
내가 이런 식으로 시도 : 나는 저자가 홀수 while 루프를 구조화 된 방법을 발견
while(inCh.read(lengthBuf) != -1) {
와 동일한 작동합니다. 저자가 그가했던 것처럼 실용적이거나 코드 명확성의 이유가 있을까요?
개인 취향에 따라 다를 수 있습니다. 변경 사항이 동일한 방식으로 작동 함을 보여줄 수 있다면 원하는 경우 사용하십시오. – Bernard
내가 볼 수있는 것은 아닙니다. –