Java NIO 之 FileChannel 的使用
2018-03-01 约 169 字
预计阅读 1 分钟
通过文件获取 FileChannel 实例
1
2
FileInputStream fis = new FileInputStream ( "/Users/peiel/a.txt" );
FileChannel fileChannel = fis . getChannel ();
2. 使用 RandomAccessFile 获取
1
2
RandomAccessFile fis = new RandomAccessFile ( "/Users/peiel/a.txt" , "rw" );
FileChannel fileChannel = fis . getChannel ();
打印读取 FileChannel 中的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ByteBuffer buf = ByteBuffer . allocate ( 4 );
int len ;
while (( len = fileChannel . read ( buf )) != - 1 ) {
// flip buf (limit = position; position = 0;)
buf . flip ();
// 方式一:转换成数组读取的方式
System . out . print ( new String ( buf . array (), 0 , len ));
// 方式二:直接读取的方式
while ( buf . hasRemaining ()) {
System . out . print (( char ) buf . get ());
}
// clear
buf . clear ();
}
写入数据到文件中
1
2
3
4
5
6
7
8
9
10
String newData = "This is a boy!" ;
ByteBuffer byteBuffer = ByteBuffer . allocate ( 16 );
byteBuffer . clear ();
byteBuffer . put ( newData . getBytes ());
byteBuffer . flip ();
while ( byteBuffer . hasRemaining ()) {
fileChannel . write ( byteBuffer );
}