NIO的用法举例
创建一个NIO的流程
- 创建FileChannel 通道
- 创建缓存Buffer,大小自己设置
- 读取数据到Buffer 注意此时的Buffer是写模式
- 切换Buffer的模式,从写到读
- 通过Buffer中的get方法读取到Buffer中的数据
- 清楚缓存关闭文件流
FileChannel的读操作例子
package NIOTest.channel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* FileChannel读取数据到buffer中
*/
public class FileChannelDemo1 {
public static void main(String[] args) throws IOException {
//创建FileChannel
RandomAccessFile accessFile = new RandomAccessFile("1.txt","rw");
FileChannel channel = accessFile.getChannel();
//创建Buffer
ByteBuffer buf = ByteBuffer.allocate(1024);
//读取数据到buffer
int read = channel.read(buf);
while (read!=-1){
System.out.println(read);
//切换buffer的读写模式
buf.flip();
System.out.println(buf);
while (buf.hasRemaining()){
System.out.println((char) buf.get());
}
//清除缓存
buf.clear();
read = channel.read(buf);
}
accessFile.close();
}
}
FileChannel写操作
package NIOTest.channel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
//FileChannel写操作
public class FileChannelDemo2 {
public static void main(String[] args) throws IOException {
//打开一个Channel通道
File file = new File("2.txt");
file.createNewFile();
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
FileChannel channel = randomAccessFile.getChannel();
String newData = "阿达顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶";
//创建Buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//清楚buffer缓存区域
byteBuffer.clear();
//向buffer中写入所需要的字节流数据
byteBuffer.put(newData.getBytes(StandardCharsets.UTF_8));
//反转byteBuffer读写模式
byteBuffer.flip();
//写入通道
while (byteBuffer.hasRemaining()){
channel.write(byteBuffer);
}
channel.close();
}
}
FileChannel基本方法
- position(): 需要在FileChannel中的某个特定位置进行数据的读写操作。可以通过调用position()方法获取FileChannel的当前位置.也可以通过调用position(long pos)方法设置FileChannel的当前位置;long pos = channel.position;channel.position(pos+123);
- size(): 返回该实例所关联文件的大小;long filesieze = channel.size();
- truncate(): 可以截取一个文件;channel.truncate(1024)
- force()方法:FileChannel方法将通道里尚未写入磁盘的数据强制写入磁盘。force()方法有一个boolean类型的参数,指明是否同时将文件元数据(权限信息等)写到磁盘上。
FileChannel两个重要方法
通道之间的数据传输:
如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel传输到另外一个channel。
transferTo
public void transferToTest() throws IOException {
//创建两个fileChannel
RandomAccessFile randomAccessFile1 = new RandomAccessFile("1.txt","rw");
FileChannel channel1 = randomAccessFile1.getChannel();
RandomAccessFile randomAccessFile2 = new RandomAccessFile("2.txt","rw");
FileChannel channel2 = randomAccessFile2.getChannel();
//从channel1 数据传输到 channel2
// channel2.transferFrom(channel1,0,(long) channel1.size());
channel1.transferTo(0,channel1.size(),channel2);
channel1.close();
channel2.close();
}
transferFrom
public static void main(String[] args) throws IOException {
//创建两个fileChannel
RandomAccessFile randomAccessFile1 = new RandomAccessFile("1.txt","rw");
FileChannel channel1 = randomAccessFile1.getChannel();
RandomAccessFile randomAccessFile2 = new RandomAccessFile("2.txt","rw");
FileChannel channel2 = randomAccessFile2.getChannel();
//从channel1 数据传输到 channel2
channel2.transferFrom(channel1,0,(long) channel1.size());
channel1.close();
channel2.close();
}