RandomaccessFile使用
RandomAccessFile是用來訪問那些保存數據記錄的文件的,你就可以用seek( )方法來訪問記錄,并進行讀寫了。這些記錄的大小不必相同;但是其大小和位置必須是可知的。但是該類僅限于操作文件。
RandomAccessFile不屬于InputStream和OutputStream類系的。實際上,除了實現DataInput和DataOutput接口之外(DataInputStream和DataOutputStream也實現了這兩個接口),它和這兩個類系毫不相干,甚至不使用InputStream和OutputStream類中已經存在的任何功能;它是一個完全獨立的類,所有方法(絕大多數都只屬于它自己)都是從零開始寫的。這可能是因為RandomAccessFile能在文件里面前后移動,所以它的行為與其它的I/O類有些根本性的不同。總而言之,它是一個直接繼承Object的,獨立的類。
我們簡單的認識一下RandomAccessFile的構造器:
RandomAccessFile raf=new RandomAccessFile(String path,String mode);
RandomAccessFile raf=new RandomAccessFile(File file, String mode);
第一個參數也就是路徑(沒有什么特別的)。
第二個參數 r 代表以只讀方式打開指定文件 (只讀) rw 以讀寫方式打開指定文件 (讀) rws 讀寫方式打開,并對內容或元數據都同步寫入底層存儲設備 rwd 讀寫方式打開,對文件內容的更新同步更新至底層存儲設備
他有幾個特色的方法:
seek(long pos):void 設置指針到pos處,在該位置可以進行讀寫。
getFilePointer():long 返回單前指針所在位置
介紹了一點點該寫一點點實例了(在文本任意處插入輸入字段)
直接上代碼:
public class Wrok_2 { public static void main(String[] args) throws IOException { File f=new File("D://Y2//S2復習//01 IO復習//test//random.txt"); insert(f, 4, "abc"); } /** * 任意位置插入內容 * @param f 文件對象 * @param seek 指針位置 * @param nei 插入內容 * @throws IOException */ static void insert(File f,long seek,String nei) throws IOException{ if(f.exists()){//是否存在 //先保存該文本中的指針所在位置之后的內容 RandomAccessFile raf=new RandomAccessFile(f, "rw");//該方法會替換之前的內容 if(seek>f.length()){ seek=0; } raf.seek(seek);//設置指針的位置 byte bs[]=new byte[1024]; String str="";//保存指針后面的數據 while(raf.read(bs)>=0){ str+=new String(bs).trim();//trim 去空格 } //開始插入內容 raf.seek(seek);//設置指針位置 為什么還要設置呢? 因為第一個seek已經失效了 raf.write(nei.getBytes());//寫入用戶輸入的字段 //System.out.PRintln(raf.getFilePointer()); raf.write(str.getBytes());//寫入指針所在位置之后的字段 System.out.println("插入內容完畢!"); raf.close();//關閉 }else{//如果沒有就創建 f.createNewFile(); insert(f, seek, nei); } } }沒有執行上面代碼之前的txt文檔內容:
執行之后:
“RandomAccessFile”實現文件的多線程下載:
public static void main(String[] args) throws IOException { /*課外: 利用“RandomAccessFile”實現文件的多線程下載*/ File f=new File("D://Y2//IO復習//01 IO復習//test//test.txt"); FileInputStream fis=new FileInputStream(f); int len=(int) (f.length()/4);//獲取該文件的大小并分成四分之一 byte bs[]=new byte[len];//準備4個容器 byte bs2[]=new byte[len]; byte bs3[]=new byte[len]; byte bs4[]=new byte[(int)f.length()-len*3]; fis.read(bs);//講讀到的數據保存到 byte容器中 fis.read(bs2); fis.read(bs3); fis.read(bs4); /*System.out.println(new String(bs)); System.out.println(new String(bs2)); System.out.println(new String(bs3)); System.out.println(new String(bs4));*/ //System.out.println(f.length());// System.out.println(a+" "+b+" "+c+" "+d); new MyTheard(len*0,bs).start();//啟動線程 new MyTheard(len*1,bs2).start(); new MyTheard(len*2,bs3).start(); new MyTheard(len*3,bs4).start(); } //類中類 static class MyTheard extends Thread{ private int seek; private byte[] bs; MyTheard(){} MyTheard(int seek, byte[] bs) { this.seek = seek; this.bs = bs; } public void run() { // TODO Auto-generated method stub RandomAccessFile raf=null; File f=new File("D://Y2//IO復習//01 IO復習//test//test1.txt"); synchronized(this){ try { raf=new RandomAccessFile(f, "rw"); raf.seek((long)this.seek); raf.write(this.bs); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally{ try { raf.close(); System.out.println("完畢"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }}如有哪里錯誤請告訴小白(我),謝謝。
新聞熱點
疑難解答