RandomAccessFile讀取文本簡介


RandomAccessFile類的常用的操作方法
1、public  RandomAccessFile(File file, String mode)throws FileNotFoundException  構造方法  接收File類的對象,指定操作路徑,但是在設置時需要設置模式:"r": 只讀、"w": 只寫、"rw": 讀寫。
2、public RandomAccessFile(String name, String mode) throws FileNotFoundException 構造方法 不再使用File類對象表示文件,而是直接輸入了一個固定的文件路徑。
3、public void close() throws IOException   關閉操作
4、public int read(byte[] b) throws IOException 將內容讀取到一個byte數組之中
5、public final byte readByte()  throws IOException 讀取一個字節
6、public final int readInt()  throws IOException從文件中讀取整型數據。
7、public void seek(long pos) throws IOException 設置讀指針的位置。
8、public final void writeBytes(String s) throws IOException 將一個字符串寫入到文件之中,按字節的方式處理。
9、public final void writeInt(int v) throws IOException 將一個int型數據寫入文件,長度為4位。
10、public int skipBytes(int n) throws IOException 指針跳過多少個字節。
構造:public RandomAccessFile(File file, String mode)  throws FileNotFoundException
實例化此類的時候需要傳遞File類,告訴程序應該操作的是哪個文件,之后有一個模式,文件的打開模式,常用的兩種模式:
r:讀模式
w:只寫
rw:讀寫,如果使用此模式,如果此文件不存在,則會自動創建。 
 
用RandomAccessFile正序讀取每一行文本
public class everyline {

	public static void main(String[] args) throws IOException, IOException {
		// TODO Auto-generated method stub
		 RandomAccessFile rf = new RandomAccessFile("E:\\databike.txt", "r");

		 String line=null;
		 while((line=rf.readLine())!=null)
		 {
			 //System.out.println(line);
			 System.out.println(new String(line.getBytes("ISO-8859-1"), "gbk"));
		 }
	}

}

讀取任意位置的數據

    /** 
     * 讀的方法 
     * @param path 文件路徑 
     * @param pointe 指針位置 
     * **/  
    public static void randomRed(String path,int pointe){  
        try{  
            //RandomAccessFile raf=new RandomAccessFile(new File("D:\\3\\test.txt"), "r");  
            /** 
             * model各個參數詳解 
             * r 代表以只讀方式打開指定文件 
             * rw 以讀寫方式打開指定文件 
             * rws 讀寫方式打開,並對內容或元數據都同步寫入底層存儲設備 
             * rwd 讀寫方式打開,對文件內容的更新同步更新至底層存儲設備 
             *  
             * **/  
            RandomAccessFile raf=new RandomAccessFile(path, "r");  
            //獲取RandomAccessFile對象文件指針的位置,初始位置是0  
            System.out.println("RandomAccessFile文件指針的初始位置:"+raf.getFilePointer());  
            raf.seek(pointe);//移動文件指針位置  
            byte[]  buff=new byte[1024];  
            //用於保存實際讀取的字節數  
            int hasRead=0;  
            //循環讀取  
            while((hasRead=raf.read(buff))>0){  
                //打印讀取的內容,並將字節轉為字符串輸入  
                System.out.println(new String(buff,0,hasRead));    
            }  
        }catch(Exception e){  
            e.printStackTrace();  
        }        
          
    }  

追加數據

/** 
 * 追加方式 
 * 寫的方法 
 * @param path 文件路徑 
 * ***/  
public static void randomWrite(String path){  
    try{  
        /**以讀寫的方式建立一個RandomAccessFile對象**/  
        RandomAccessFile raf=new RandomAccessFile(path, "rw");  
          
        //將記錄指針移動到文件最后  
        raf.seek(raf.length());  
        raf.write("我是追加的 \r\n".getBytes());  
          
    }catch(Exception e){  
        e.printStackTrace();  
    }  
      
}  

任意位置插入數據

/** 
     * 實現向指定位置 
     * 插入數據 
     * @param fileName 文件名 
     * @param points 指針位置 
     * @param insertContent 插入內容 
     * **/  
    public static void insert(String fileName,long points,String insertContent){  
        try{  
        File tmp=File.createTempFile("tmp", null);  
        tmp.deleteOnExit();//在JVM退出時刪除  
          
        RandomAccessFile raf=new RandomAccessFile(fileName, "rw");  
        //創建一個臨時文件夾來保存插入點后的數據  
        FileOutputStream tmpOut=new FileOutputStream(tmp);  
        FileInputStream tmpIn=new FileInputStream(tmp);  
        raf.seek(points);  
        /**將插入點后的內容讀入臨時文件夾**/  
          
        byte [] buff=new byte[1024];  
        //用於保存臨時讀取的字節數  
        int hasRead=0;  
        //循環讀取插入點后的內容  
        while((hasRead=raf.read(buff))>0){  
            // 將讀取的數據寫入臨時文件中  
            tmpOut.write(buff, 0, hasRead);  
        }  
          
        //插入需要指定添加的數據  
        raf.seek(points);//返回原來的插入處  
        //追加需要追加的內容  
        raf.write(insertContent.getBytes());  
        //最后追加臨時文件中的內容  
        while((hasRead=tmpIn.read(buff))>0){  
            raf.write(buff,0,hasRead);  
        }  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
    }  

  
  我們可以用RandomAccessFile這個類,來實現一個多線程斷點下載的功能,用過下載工具的朋友們都知道,下載前都會建立兩個臨時文件,一個是與被下載文件大小相同的空文件,另一個是記錄文件指針的位置文件,每次暫停的時候,都會保存上一次的指針,然后斷點下載的時候,會繼續從上一次的地方下載,從而實現斷點下載或上傳的功能,有興趣的朋友們可以自己實現下。

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM