RandomaccessFile使用
RandomAccessFile是用來訪問那些保存數(shù)據(jù)記錄的文件的,你就可以用seek( )方法來訪問記錄,并進(jìn)行讀寫了。這些記錄的大小不必相同;但是其大小和位置必須是可知的。但是該類僅限于操作文件。
RandomAccessFile不屬于InputStream和OutputStream類系的。實(shí)際上,除了實(shí)現(xiàn)DataInput和DataOutput接口之外(DataInputStream和DataOutputStream也實(shí)現(xiàn)了這兩個(gè)接口),它和這兩個(gè)類系毫不相干,甚至不使用InputStream和OutputStream類中已經(jīng)存在的任何功能;它是一個(gè)完全獨(dú)立的類,所有方法(絕大多數(shù)都只屬于它自己)都是從零開始寫的。這可能是因?yàn)镽andomAccessFile能在文件里面前后移動(dòng),所以它的行為與其它的I/O類有些根本性的不同。總而言之,它是一個(gè)直接繼承Object的,獨(dú)立的類。
我們簡單的認(rèn)識(shí)一下RandomAccessFile的構(gòu)造器:
RandomAccessFile raf=new RandomAccessFile(String path,String mode);
RandomAccessFile raf=new RandomAccessFile(File file, String mode);
第一個(gè)參數(shù)也就是路徑(沒有什么特別的)。
第二個(gè)參數(shù) r 代表以只讀方式打開指定文件 (只讀) rw 以讀寫方式打開指定文件 (讀) rws 讀寫方式打開,并對(duì)內(nèi)容或元數(shù)據(jù)都同步寫入底層存儲(chǔ)設(shè)備 rwd 讀寫方式打開,對(duì)文件內(nèi)容的更新同步更新至底層存儲(chǔ)設(shè)備
他有幾個(gè)特色的方法:
seek(long pos):void 設(shè)置指針到pos處,在該位置可以進(jìn)行讀寫。
getFilePointer():long 返回單前指針?biāo)谖恢?/strong>
介紹了一點(diǎn)點(diǎn)該寫一點(diǎn)點(diǎn)實(shí)例了(在文本任意處插入輸入字段)
直接上代碼:
public class Wrok_2 { public static void main(String[] args) throws IOException { File f=new File("D://Y2//S2復(fù)習(xí)//01 IO復(fù)習(xí)//test//random.txt"); insert(f, 4, "abc"); } /** * 任意位置插入內(nèi)容 * @param f 文件對(duì)象 * @param seek 指針位置 * @param nei 插入內(nèi)容 * @throws IOException */ static void insert(File f,long seek,String nei) throws IOException{ if(f.exists()){//是否存在 //先保存該文本中的指針?biāo)谖恢弥蟮膬?nèi)容 RandomAccessFile raf=new RandomAccessFile(f, "rw");//該方法會(huì)替換之前的內(nèi)容 if(seek>f.length()){ seek=0; } raf.seek(seek);//設(shè)置指針的位置 byte bs[]=new byte[1024]; String str="";//保存指針后面的數(shù)據(jù) while(raf.read(bs)>=0){ str+=new String(bs).trim();//trim 去空格 } //開始插入內(nèi)容 raf.seek(seek);//設(shè)置指針位置 為什么還要設(shè)置呢? 因?yàn)榈谝粋€(gè)seek已經(jīng)失效了 raf.write(nei.getBytes());//寫入用戶輸入的字段 //System.out.PRintln(raf.getFilePointer()); raf.write(str.getBytes());//寫入指針?biāo)谖恢弥蟮淖侄? System.out.println("插入內(nèi)容完畢!"); raf.close();//關(guān)閉 }else{//如果沒有就創(chuàng)建 f.createNewFile(); insert(f, seek, nei); } } }沒有執(zhí)行上面代碼之前的txt文檔內(nèi)容:
執(zhí)行之后:
“RandomAccessFile”實(shí)現(xiàn)文件的多線程下載:
public static void main(String[] args) throws IOException { /*課外: 利用“RandomAccessFile”實(shí)現(xiàn)文件的多線程下載*/ File f=new File("D://Y2//IO復(fù)習(xí)//01 IO復(fù)習(xí)//test//test.txt"); FileInputStream fis=new FileInputStream(f); int len=(int) (f.length()/4);//獲取該文件的大小并分成四分之一 byte bs[]=new byte[len];//準(zhǔn)備4個(gè)容器 byte bs2[]=new byte[len]; byte bs3[]=new byte[len]; byte bs4[]=new byte[(int)f.length()-len*3]; fis.read(bs);//講讀到的數(shù)據(jù)保存到 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();//啟動(dòng)線程 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復(fù)習(xí)//01 IO復(fù)習(xí)//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(); } } } } }}如有哪里錯(cuò)誤請告訴小白(我),謝謝。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注