RandomAccessFile 隨機讀取文件內(nèi)容
構造方法 RandomAccessiFile raf=new RandomAccessFile("filename",mode);
讀取任意位置的文件內(nèi)容
-
兩個方法
獲得文件指針當前指向的位置: raf.getfilepoint();設置文件指針的位置: raf.seek(int position);
RandomAccessFile和輸入輸出流類似使用Read()和Write()方法
在指定位置進行內(nèi)容的插入內(nèi)容,需要將指針位置以后內(nèi)容放在另外的文件進行保存或者 寫入到Buffer中進行保存,否則插入的內(nèi)容會覆蓋之前的
Buffer
多種進本數(shù)據(jù)類型的實現(xiàn)類,主要的實現(xiàn)類ByteBuffer,CharBuffer
主要方法put(),get()
構造方法
通過allocate(int capacity)方法獲得
ByteBuffer bytebuffer=ByteBuffer.allocate(10);初始化容量為10主要參數(shù)
position,limit,capacity-
讀取和輸出
輸入
positon初始值是0,limit進行數(shù)據(jù)的封裝
添加數(shù)據(jù)完畢以后調(diào)用byteBufer.flip()方法,為輸出做準備輸出
byteBuffer.clear()調(diào)用進行數(shù)據(jù)讀入做準備 -
簡單例子
public class BufferTest {
@Test
public void initTest(){
CharBuffer buffer=CharBuffer.allocate(8);
System.out.println("capacity="+buffer.capacity());
System.out.println("position="+buffer.position());
System.out.println("limit="+buffer.limit());System.out.println("\n 添加元素以后....."); buffer.put('s'); buffer.put('h'); buffer.put('f'); System.out.println("position="+buffer.position()); System.out.println("capacity="+buffer.capacity()); System.out.println("limit="+buffer.limit()); System.out.println("\n 添加元素結束以后準備取出元素...."); buffer.flip(); System.out.println("position="+buffer.position()); System.out.println("limit="+buffer.limit()); System.out.println("capacity="+buffer.capacity()); System.out.println("\n 取出元素以后......"); System.out.println("取出第一個元素buffer[0]="+buffer.get()); System.out.println("position="+buffer.position()); System.out.println("capacity="+buffer.capacity()); System.out.println("limit="+buffer.limit()); System.out.println("\n 取出元素以后為添加元素做準備...."); buffer.clear(); System.out.println("position="+buffer.position()); System.out.println("limit="+buffer.limit()); System.out.println("capacity="+buffer.capacity()); }
Channel
主要的實現(xiàn)類FileChannel等
類似于輸入輸出流
指定文件中的內(nèi)容映射到buffer中
FileChannel進行映射
** 映射成ByteBuffer **
MapedByBuffer bytebuffer=FileChannel.map()不能直接對Channel進行讀取和寫入, 必須Buffer
構造方法,通過文件流進行獲得
FileChannel in=Fileinputstream(new File()).getChannel();
FileChannel out=FileoutputStream(new File()).getChannel();讀取
MapedBybyteBuffer bytebuffer=in.map(FileChannel.mapmodel.read_only,0,file.length);-
文件復制
public class ChannelTest {
@Test
public void changeTest() throws IOException{
/*
* 獲取類似文件流的通道channel
*/File file=new File("E:/javaFile/shf.txt"); FileChannel inChannel=new FileInputStream(file).getChannel(); FileChannel outChannel=new FileOutputStream(new File("E:/javaFile/haifeng.txt")).getChannel(); /* * 將Channel數(shù)據(jù)映射到byteBuffer中 */ MappedByteBuffer buffer=inChannel.map(FileChannel.MapMode.READ_ONLY, 0,file.length()); /* * 將buffer中的數(shù)據(jù)直接輸出到文件中 */ outChannel.write(buffer); /* * 不能講buffer內(nèi)容直接輸出 * 需要進行解碼器進行解碼 * 轉(zhuǎn)換成charBuffer * 進行輸出 */ //System.out.println(buffer); /* * 必須將buffer進行清空 * 為取出內(nèi)容做準備 * buffer.clear() */ buffer.clear(); Charset charset=Charset.forName("utf-8"); CharsetDecoder decoder=charset.newDecoder(); CharBuffer charBuffer=decoder.decode(buffer); System.out.println(charBuffer); } }