package c.ct.io.nio;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NioDemo {
public static void main(String[] args) throws IOException {
// write();
// read();
// m3();
// m4();
// m5();
m6();
}
public static void m6() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
//存入数据
buffer.put((byte) 'A');
buffer.put((byte) 'B');
buffer.put((byte) 'C');
buffer.put((byte) 'D');
buffer.compact();
System.out.print(buffer.capacity() + "\t");
System.out.print(buffer.limit() + "\t");
System.out.println(buffer.position() + "\t");
buffer.put((byte) 'E');
buffer.put((byte) 'F');
System.out.print(buffer.capacity() + "\t");
System.out.print(buffer.limit() + "\t");
System.out.println(buffer.position() + "\t");
buffer.flip();
System.out.print(buffer.capacity() + "\t");
System.out.print(buffer.limit() + "\t");
System.out.println(buffer.position() + "\t");
System.out.println(buffer.get(1020));
System.out.println(buffer.get(1021));
System.out.println(buffer.get(0));
System.out.println(buffer.get(1022));//Exception in thread "main" java.lang.IndexOutOfBoundsException
}
public static void m5() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
//存入数据
buffer.put((byte) 'A');
buffer.put((byte) 'B');
buffer.compact();
System.out.println(buffer.capacity());
System.out.println(buffer.limit());
System.out.println(buffer.position());
buffer.put((byte) 'C');
buffer.put((byte) 'D');
System.out.println(new String(buffer.array()));
System.out.println(buffer.capacity());
System.out.println(buffer.limit());
System.out.println(buffer.position());
buffer.flip();
System.out.println((char) buffer.get(1021));
System.out.println((char) buffer.get(1022));
System.out.println((char) buffer.get(1023));
System.out.println("压缩后get:" + new String(buffer.array()));
}
public static void m4() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put((byte) 'A');
buffer.put((byte) 'B');
System.out.println(buffer.capacity());
System.out.println(buffer.limit());
System.out.println(buffer.position());
buffer.flip();
System.out.println(buffer.capacity());
System.out.println(buffer.limit());
System.out.println(buffer.position());
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
}
public static void m3() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
System.out.println("写入之前");
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
// System.out.println("标记: "+buffer.mark());
buffer.put("hello".getBytes());
System.out.println("写入之后");
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
// System.out.println("标记: "+buffer.mark());
buffer.flip();
System.out.println("改为读取模式");
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
System.out.println("读取之后");
byte[] buf = new byte[2];
buffer.get(buf);
String s = new String(buf, 0, 2);
System.out.println(s);
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
System.out.println("------rewind之后--------");
buffer.rewind();
System.out.println("==================");
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
System.out.println("------读取2个字节--------");
buffer.get(buf);
String ste = new String(buf);
System.out.println(ste);
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
System.out.println("-------------compact之后------------");
buffer.compact();//将没读到两个的放到前两位 当前位置不变
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
System.out.println("before flip");
buffer.flip();
System.out.println("容量:" + buffer.capacity());
System.out.println("限制:" + buffer.limit());
System.out.println("位置:" + buffer.position());
String s6 = new String(buffer.array(), 0, 3);
System.out.println(s6);
}
public static void write() throws FileNotFoundException, UnsupportedEncodingException {
FileOutputStream fileOutputStream = new FileOutputStream("src/nio.txt", true);
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer buf = ByteBuffer.allocate(100);
buf.put("hello\r\n".getBytes("UTF-8"));
try {
buf.flip();
fileChannel.write(buf);
fileChannel.close();
fileOutputStream.close();
} catch (IOException e) {
}
}
public static void read() {
RandomAccessFile aFile = null;
try {
aFile = new RandomAccessFile("src/nio.txt", "rw");
FileChannel fileChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(100);
while (fileChannel.read(buf) != -1) {
buf.flip();
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
buf.clear();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (aFile != null) {
aFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
有部分参考csdn,具体链接不知道了,如有侵权,请告知删除。