使用ByteBuffer和Channel将字符串写入文件
public class NIOFileChannel01 {
public static void main(String[] args) throws Exception{
String str = "hello,Mz";
FileOutputStream fileOutputStream = new FileOutputStream("f:\\file01.txt");
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put(str.getBytes());
byteBuffer.flip();
fileChannel.write(byteBuffer);
fileOutputStream.close();
}
}
使用Channel和ByteBuffer将文件读取为字符串
public class NIOFileChannel02 {
public static void main(String[] args) throws Exception {
File file = new File("f:\\file01.txt");
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
fileChannel.read(byteBuffer);
System.out.println(new String(byteBuffer.array()));
fileInputStream.close();
}
}
使用两个Channel和一个ByteBuffer进行文件拷贝
public class NIOFileChannel03 {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("1.txt");
FileChannel fileChannel01 = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
FileChannel fileChannel02 = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(512);
while (true) {
byteBuffer.clear();
int read = fileChannel01.read(byteBuffer);
System.out.println("read =" + read);
if(read == -1) {
break;
}
byteBuffer.flip();
fileChannel02.write(byteBuffer);
}
fileInputStream.close();
fileOutputStream.close();
}
}
使用transferFrom进行文件拷贝
public class NIOFileChannel04 {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("d:\\a1.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");
FileChannel source = fileInputStream.getChannel();
FileChannel target = fileOutputStream.getChannel();
target.transferFrom(source,0,source.size());
source.close();
target.close();
fileInputStream.close();
fileOutputStream.close();
}
}
Buffer的get和put顺序要一致(否则可能报异常)
public class NIOByteBufferPutGet {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(64);
buffer.putInt(100);
buffer.putLong(9);
buffer.putChar('尚');
buffer.putShort((short) 4);
buffer.flip();
System.out.println();
System.out.println(buffer.getInt());
System.out.println(buffer.getLong());
System.out.println(buffer.getLong());
System.out.println(buffer.getShort());
}
}
ReadOnlyBuffer注意事项
public class ReadOnlyBuffer {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(64);
for(int i = 0; i < 64; i++) {
buffer.put((byte)i);
}
buffer.flip();
ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
System.out.println(readOnlyBuffer.getClass());
while (readOnlyBuffer.hasRemaining()) {
System.out.println(readOnlyBuffer.get());
}
readOnlyBuffer.put((byte)100);
}
}
MappedByteBuffer的使用
public class MappedByteBufferTest {
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw");
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
mappedByteBuffer.put(0, (byte) 'H');
mappedByteBuffer.put(3, (byte) '9');
mappedByteBuffer.put(5, (byte) 'Y');
randomAccessFile.close();
System.out.println("修改成功~~");
}
}
ScatteringAndGathering(使用Buffer数组的方式读取)
public class ScatteringAndGatheringTest {
public static void main(String[] args) throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
InetSocketAddress inetSocketAddress = new InetSocketAddress(7000);
serverSocketChannel.socket().bind(inetSocketAddress);
ByteBuffer[] byteBuffers = new ByteBuffer[2];
byteBuffers[0] = ByteBuffer.allocate(5);
byteBuffers[1] = ByteBuffer.allocate(3);
SocketChannel socketChannel = serverSocketChannel.accept();
int messageLength = 8;
while (true) {
int byteRead = 0;
while (byteRead < messageLength ) {
long l = socketChannel.read(byteBuffers);
byteRead += l;
System.out.println("byteRead=" + byteRead);
Arrays.asList(byteBuffers).stream().map(buffer -> "postion=" + buffer.position() + ", limit=" + buffer.limit()).forEach(System.out::println);
}
Arrays.asList(byteBuffers).forEach(buffer -> buffer.flip());
long byteWirte = 0;
while (byteWirte < messageLength) {
long l = socketChannel.write(byteBuffers);
byteWirte += l;
}
Arrays.asList(byteBuffers).forEach(buffer-> {
buffer.clear();
});
System.out.println("byteRead:=" + byteRead + " byteWrite=" + byteWirte + ", messagelength" + messageLength);
}
}
}