java API中提供了一個基于指針操作實現對文件隨機訪問操作的類,該類就是RandomAccessFile類,該類不同于其他很多基于流方式讀寫文件的類。它直接繼承自Object。
1
|
public class RandomAccessFile extends Objectimplements DataOutput, DataInput, Closeable{...} |
1.使用該類時可以指定對要操作文件的讀寫模式。
第一種模式是只讀模式,第二種模式是讀寫模式。在創建該類實例時指定。
1
2
|
@Test public void test01() throws IOException{ |
1
2
3
4
5
6
7
8
|
//讀寫模式 RandomAccessFile r= new RandomAccessFile( new File( "" ), "rw" ); //只讀模式 //<span style="font-family:Arial, Helvetica, sans-serif;">RandomAccessFile r=new RandomAccessFile(new File(""),"r" );</span> r.write( 1 ); //寫出一個字節,寫出的是int值的低8位 r.close(); r.read(); //每次讀一個字節,填充到int的低八位 } |
2.字節讀取操作
(1)void write(int d):寫出該int值的低8位,其他位舍棄
1
2
3
4
5
6
7
|
@Test public void testWrite() throws IOException{ RandomAccessFile file= new RandomAccessFile( new File( "emp.txt" ), "rw" ); file.write( 97 ); //0110 0001 file.write( 98 ); //0110 0010 file.write( 99 ); //0110 0011 } |
(2)int read():注意:該方法只會從文件中當前指針處讀取一個byte(8位)的數據填充到int返回值的第八位,其他位用0填充。若該方法返回0,則代表讀到文件末尾。
以上兩種方法使用起來當然會很麻煩。
RandomAccessFile類中還封裝了對8中基本數據類型的讀寫操作,使用起來會很方便。
分別為
readByte(), readShort(), readInt(), readLong(), readFloat(),readDouble(),readBoolean(),readChar()
返回值類型均為對應的基本數據類型。同時相應的也有這八中writeByte()...方法。這是在流式讀寫文件中所不具有的。
3.文件指針操作
RandomAccessFile類的所有讀寫操作均是基于指針的,指針會隨著讀或寫的操作往后移動。同時也可以通過方法自由的操作指針的位置,以此更方便的讀寫文件。
常用方法
(1)long getFilePointer():該方法用于返回當前指針位置。默認讀寫操作時,指針的初始位置為文件的第一個字節處,即值為0
(2)void seek(long pos):該方法可以設定指針位置
(3)int skipBytes(int n):該方法可以跳過n個字節
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
/** * RandomAccessFile:基于指針讀寫,總是在指針當前位置讀寫,無論讀寫,指針都會向后移動 * RandomAccessFile總是在指針當前位置進行讀寫字節,并且無論進行了讀還是寫一個字節后, * 指針都會自動向后移動到下一個字節的位置 * 默認創建出來RandomAccessFile時,指針位置為0,即:文件的第一個字節的位置 * @author zc */ public class T13RandomAccessFile { public static void main(String[] args) throws IOException { RandomAccessFile raf= new RandomAccessFile( new File( "emp.txt" ), "rw" ); int a= 255 ; //雖然能寫入,但是在記事本中打開仍是亂碼字符 raf.write(a>>> 24 ); //11111111 raf.write(a>>> 16 ); raf.write(a>>> 8 ); raf.write(a); //獲取當前指針位置 long pos=raf.getFilePointer(); //!!注意,返回值是字節4,前面寫入的四個字節對應從0--3字節 System.out.println( "pos:" +pos); //4 //將int值分成四部分,寫入 raf.writeInt( 255 ); System.out.println( "pos:" +raf.getFilePointer()); //8 raf.writeLong( 255 ); //long八個字節 System.out.println( "pos:" +raf.getFilePointer()); //16 raf.writeFloat( 8 ); //float四個字節 System.out.println( "pos:" +raf.getFilePointer()); //20 raf.writeDouble( 12.1 ); //double八個字節 System.out.println( "pos:" +raf.getFilePointer()); //28 raf.write( new byte []{ 1 , 1 }); raf.writeBoolean( false ); raf.writeChar( 1 ); //此時已經寫完,指針指向文件某位 System.out.println(raf.read()); //-1 /* *void seek(long pos) *將指針移動到指定位置 * */ raf.seek( 0 ); System.out.println( "point:" +raf.getFilePointer()); //0 //讀取剛剛寫入的long值 raf.seek( 8 ); long l=raf.readLong(); System.out.println( "讀出的long值:" +l); //255 //讀取剛剛寫入的double值 raf.seek( 20 ); System.out.println(raf.readDouble()); //12.1 raf.close(); } } |
總結
以上就是本文關于java使用RandomAccessFile類基于指針讀寫文件實例代碼的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/kaishizhangcheng/article/details/52463850