Buffer
- ByteBuffer byteBuffer;
- CharBuffer charBuffer;
- ShortBuffer shortBuffer;
- IntBuffer intBuffer;
- LongBuffer longBuffer;
- FloatBuffer floatBuffer;
DoubleBuffer doubleBuffer;
String data = "hellonio";
// 创建 1024是缓冲区大小
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//元素放进去
byteBuffer.put(data.getBytes());
byteBuffer.flip();//切换模式 读/写
byteBuffer.limit(); //写模式下是能写多少数据数据 读模式下是能读的数据长度大小
byteBuffer.position();//写模式下是写的位置 读模式下是读的内容下标 默认从0开始读
byteBuffer.capacity();//读写模式下都是缓冲区容量
//把读取的数据放到buff中 读取0-3的数据
byte[] buff = new byte[limit];
byteBuffer.get(buff, 0, 3);
Channel
常规拷贝文件
系统内核读取文件存放到内存中,然后用户进程去内核内存中,复制系统读取到的内容这才完成文件拷贝。
直接内存拷贝
用户和内核共享一块内存这样就避免了二次操作
一般用于拷贝大文件
public static void t1() throws Exception {
FileInputStream fis = new FileInputStream("f:/1.txt");
FileOutputStream fos = new FileOutputStream("f:/121.txt");
FileChannel inChannel = fis.getChannel();
FileChannel outChannel = fos.getChannel();
ByteBuffer buff = ByteBuffer.allocate(1024);
while (inChannel.read(buff) != -1) {
// 切换读模式
buff.flip();
outChannel.write(buff);
buff.clear();
}
outChannel.close();
inChannel.close();
fos.close();
fis.close();
}
内存映射文件拷贝
//读 把153M的文件装载到内存中花费87毫秒
public static void t2() throws Exception {
FileInputStream fis = new FileInputStream("f:/20200604_164622.mp4");
FileChannel inChannel = fis.getChannel();//NIO的通道
// 内存映射 读 申请空间0-文件大小
MappedByteBuffer mappedByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
long fileLen = inChannel.size();//文件大小
byte[] buff = new byte[(int) fileLen];//文件大小的数组
long start = System.currentTimeMillis();
mappedByteBuffer.get(buff);
long end = System.currentTimeMillis();
System.out.println("use time = " + (end - start));
}
-
通道传输
需要系统支持,如果不支持就采用内存映射的方式传输 ```java //拷贝花费104毫秒 public static void t4() throws Exception { //开辟通道 FileChannel inChannel = FileChannel.open(Paths.get(“f:/20200604164622.mp4”), StandardOpenOption.READ); FileChannel outChannel = FileChannel.open(Paths.get(“f:/20200604_164622.mp4”)
, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
long start = System.currentTimeMillis(); // transferToDirectly // transferToTrustedChannel // transferToArbitraryChannel //读取的文件传到通道里面去 inChannel.transferTo(0, inChannel.size(), outChannel); long end = System.currentTimeMillis(); System.out.println(“use time = “ + (end - start)); inChannel.close(); outChannel.close();
}
<a name="Gk3MF"></a>
### 分散和聚集
```java
public static void t5() throws Exception {
FileInputStream fis = new FileInputStream("f:/1.txt");
FileChannel inChannel = fis.getChannel();
//创建缓冲区
ByteBuffer header = ByteBuffer.allocate(5);
ByteBuffer body = ByteBuffer.allocate(10);
// 12345678901234567890
// header = 12345
// body = 6789012345
ByteBuffer[] arr = {header, body};
inChannel.read(arr);
// 切换读模式
//循环读取 先读取5个字节 在读取10个字节一共15个没有读取到的内容就没读取到
for (ByteBuffer bb : arr) {
bb.flip();
System.out.println(new String(bb.array(), 0, bb.limit()));
}
//写入
FileOutputStream fos = new FileOutputStream("f:/111222.txt");
FileChannel outChannel = fos.getChannel();
outChannel.write(arr);
outChannel.close();
}
截断
//直接把原内容截了截取了7个
public static void t6() throws Exception {
FileOutputStream fos = new FileOutputStream("f:/111222.txt", true);
FileChannel outChannel = fos.getChannel();
outChannel.truncate(7);
}
加锁
FileLock对当前文件加锁解决多线程问题