?? randomfilechanneltest.java
字號:
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
/**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class RandomFileChannelTest
{
public static void main(String[] args)
{
FileChannel randomChannel = null;
try
{
File f = new File("a.txt");
//創建一個RandomAccessFile對象
RandomAccessFile raf = new RandomAccessFile(f, "rw");
//獲取RandomAccessFile對應的Channel
randomChannel = raf.getChannel();
//將Channel中所有數據映射成ByteBuffer
ByteBuffer buffer = randomChannel.map(FileChannel.MapMode.READ_ONLY,
0 , f.length());
//把Channel的記錄指針移動到最后
randomChannel.position(f.length());
//將buffer中所有數據輸出
randomChannel.write(buffer);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (randomChannel != null)
randomChannel.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -