java nio의 주요 클래스인 ByteBuffer 클래스의 주요 메소드를 이해하기 위한 코드
src
ByteBuffer buffer = ByteBuffer.allocate(10); buffer.put((byte) 0); buffer.put((byte) 1); System.out.println("put 0,1 - " + buffer + ", remain=" + buffer.remaining()); buffer.mark(); System.out.println("mark - " + buffer + ", remain=" + buffer.remaining()); buffer.put((byte) 2); buffer.put((byte) 3); System.out.println("put 2,3 - " + buffer + ", remain=" + buffer.remaining()); buffer.reset(); System.out.println("reset - " + buffer + ", remain=" + buffer.remaining()); buffer.flip(); System.out.println("flip - " + buffer + ", remain=" + buffer.remaining()); FileOutputStream fos = new FileOutputStream("c:/z/a.txt"); FileChannel fcOut = fos.getChannel(); fcOut.write(buffer); System.out.println("write to file channel a.txt - " + buffer + ", remain=" + buffer.remaining()); fcOut.close(); buffer.rewind(); System.out.println("rewind - " + buffer + ", remain=" + buffer.remaining()); fos = new FileOutputStream("c:/z/b.txt"); fcOut = fos.getChannel(); fcOut.write(buffer); System.out.println("write to file channel b.txt - " + buffer + ", remain=" + buffer.remaining()); fcOut.close();
result
put 0,1 - java.nio.HeapByteBuffer[pos=2 lim=10 cap=10], remain=8 mark - java.nio.HeapByteBuffer[pos=2 lim=10 cap=10], remain=8 put 2,3 - java.nio.HeapByteBuffer[pos=4 lim=10 cap=10], remain=6 reset - java.nio.HeapByteBuffer[pos=2 lim=10 cap=10], remain=8 flip - java.nio.HeapByteBuffer[pos=0 lim=2 cap=10], remain=2 write to file channel a.txt - java.nio.HeapByteBuffer[pos=2 lim=2 cap=10], remain=0 rewind - java.nio.HeapByteBuffer[pos=0 lim=2 cap=10], remain=2 write to file channel b.txt - java.nio.HeapByteBuffer[pos=2 lim=2 cap=10], remain=0
'java' 카테고리의 다른 글
java.io.File.getCanonicalPath(), getAbsolutePath() (0) | 2008.03.05 |
---|---|
제9회 자바 개발자 컨퍼런스 (0) | 2008.02.04 |
java System.in에서 키보드 입력을 받을때 에코되는 문자를 안보이게 처리하기 (0) | 2007.11.16 |
숫자 형식에 대해서 java 정규식 테스트 (1) | 2007.07.24 |
csv 포맷 그리고 java (0) | 2007.06.25 |