私はJavaに関する本を読んでいて、チャネルから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) 壊す; lengthBuf.flip(); strLength =(int)lengthBuf.getDouble(); buffers [0] = ByteBuffer.allocate(2 * strLength); if(inCh.read(buffers)== -1){ System.err.println( "EOFがhteprime文字列の読み取りを検出しました。"); 壊す; } System.out.printf( "文字列の長さ:%3s文字列:%-12sバイナリ値:%3d%n"、strLength、 ((ByteBuffer)(buffers [0] .flip()))。asCharBuffer()。toString()、 ((ByteBuffer)buffers [1] .flip())。getLong()); lengthBuf.clear(); buffers [1] .clear(); } System.out.println( "\ nEOFに到達しました。"); } catch(IOException e){
私はこのようにそれを試しました:
while(inCh.read(lengthBuf) != -1) {
そしてそれは同じように機能します。作者が彼のようにそれを書くという実用的またはコードの明確さの理由はありますか?