2020.8.7第三十二天
阿新 • • 發佈:2020-08-07
1.今天學習RandomAccessFile隨機訪問檔案
RandomAccessFile可以隨機讀寫檔案,隨機讀寫檔案就是說可以任意訪問檔案的位置,
這是其他流所不能操縱的。RandomAccessFile類包含一個記錄指標,用於標識當前流的讀
寫位置,這個位置可以向前移動,也可以向後移動。RandomAccessFile包含兩個方法來操
作檔案記錄指標。
long getFilePoint():記錄檔案指標的當前位置。
void seek(long pos):將檔案記錄指標定位到pos位置。
public RandomAccessPile(Eile file,String mode) throwsFileNotFoundException public RandomAccessFile(String name,String mode) throws FileNotFoundException
1 import java.io.IOException; 2 import java.io.RandomAccessFile; 3 public class RandomAccessFileDemo { 4 public static void main (String[] args)throws IOException { 5 RandomAccessFile acf= 6new RandomAccessFile("D:/Hello.txt", "rw"); 7 byte[] b = "Hello Java!!".getBytes(); 8 acf.write(b); 9 acf.seek(6); 10 System.out.println ("pointer="+acf.getFilePointer()); 11 byte[] b2="C++".getBytes(); 12 acf.write(b2); 13 int len=-1; 14 byte[] buf=new byte[1024]; 15 acf.seek(0); 16 System.out.println ("pointer="+acf.getFilePointer());17 while((len=acf.read (buf))!=-1) { 18 String s =new String(buf,0,len); 19 System.out.println(s); 20 } 21 } 22 }
2.遇到的問題:不知道如何定位指標
3.明天覆習第12章。