1.使用同一个byteBuffer实现内容拷贝
package com.netty.bufferandchannel;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author Fighting
* @version BufferChannelReadWrite, v 0.1 2020/8/12 19:54 Fighting
* @Content 使用一个buffer实现将一个文件中的数据拷贝到另一个文件
*/
public class BufferChannelReadWrite {
public static void main(String[] args) throws IOException {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
File file = new File("G:\\netty-workspace\\netty-test\\src\\main\\java\\com\\netty\\bufferandchannel\\demo.txt");
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel readchannel = fileInputStream.getChannel();
readchannel.read(byteBuffer);
System.out.println("从文件中读取的数据为:"+new String(byteBuffer.array()));
FileOutputStream fileOutputStream = new FileOutputStream("G:\\netty-workspace\\netty-test\\src\\main\\java\\com\\netty\\bufferandchannel\\demoCopy.txt");
FileChannel writeChannel = fileOutputStream.getChannel();
byteBuffer.flip();
writeChannel.write(byteBuffer);
fileInputStream.close();
fileOutputStream.close();
}
}
2.byteBuffer只读
package com.netty.bufferandchannel;
import java.nio.ByteBuffer;
/**
* @author Fighting
* @version ByteBufferReadOnly, v 0.1 2020/8/12 22:54 Fighting
* @Content 只读模式下的buffer只能是读取数据,不能进行写数据的操作
* Exception in thread "main" java.nio.ReadOnlyBufferException
*/
public class ByteBufferReadOnly {
public static void main(String[] args) {
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
// byteBuffer.put(("abc").getBytes());
// byteBuffer.put(("abc").getBytes());
// byteBuffer.put(("abc").getBytes());
// byteBuffer.put(("abc").getBytes());
for (int i = 0; i < 64; i++) {
byteBuffer.put((byte)i);
}
byteBuffer.flip();
ByteBuffer buffer = byteBuffer.asReadOnlyBuffer();
while(byteBuffer.hasRemaining()){
System.out.println(byteBuffer.get());
}
buffer.put("123".getBytes());
}
}
3.byteBuffer存入什么类型的数据,取出的时候还需要用对应的类型取出,否则报错
package com.netty.bufferandchannel;
import java.nio.ByteBuffer;
/**
* @author Fighting
* @version ByteBufferTestData, v 0.1 2020/8/12 22:55 Fighting
* @Content Bytebuffer中存入什么类型的数据,在取出的时候就按照什么类型的方式取出
* 如果取出的时候类型不对应,则会报出BufferUnderflowException
*/
public class ByteBufferTestData {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(20);
buffer.putInt(1);
buffer.putDouble(12L);
buffer.putShort((short)4);
buffer.put("abc".getBytes());
buffer.flip();
System.out.println(buffer.getInt());
System.out.println(buffer.getDouble());
System.out.println(buffer.getShort());
System.out.println(buffer.getDouble());
System.out.println(buffer.get());
}
}
4.使用byteBuffer的子类MappedByteBuffer[DirectByteBuffer]实现在操作系统级别操作数据
package com.netty.bufferandchannel;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author Fighting
* @version MappedByteBufferDemo, v 0.1 2020/8/12 22:52 Fighting
* @Content 直接操作内存数据
*/
public class MappedByteBufferDemo {
public static void main(String[] args) throws Exception{
RandomAccessFile randomAccessFile = new RandomAccessFile("G:\\netty-workspace\\netty-test\\src\\main\\java\\com\\netty\\bufferandchannel\\demo.txt", "rw");
//10是可以操作多少个
MappedByteBuffer map = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 10);
map.put(0,(byte)'M');
map.put(9,(byte)'~');
randomAccessFile.close();
System.out.println("操作内存完成...");
}
}