1. 程式人生 > >JAVA高階基礎(40)---RandomAccessFile

JAVA高階基礎(40)---RandomAccessFile

RandomAccessFile

隨機訪問檔案。可以通過在建立物件的時候,指定模式:r 讀模式  /  rw  寫模式

在其中存在指標,來定位對檔案進行讀寫的位置

  • getPointFile(); 獲取指標位置
  • seek(long  pos )設定指標位置

在讀取檔案的時候,我們要注意一問題:是否存在讀取一個字串的方式。在讀的時候,要注意轉換為字串的時候的編碼方式
readLine();但是該方法的預設的解碼方式時ISO-8859-1

注:具體方法請查詢API

相關程式碼

package org.lanqiao.randomaccessfile.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile  raf = new RandomAccessFile("aa.txt","r");
		//獲取指標的位置
		System.out.println(raf.getFilePointer());// 0  開始指向檔案的開始位置
		String strLine  =  raf.readLine();
		System.out.println(raf.getFilePointer());
		System.out.println(strLine);
		//因為讀的時候使用的ISO-8859-1   需要重寫進行解碼和編碼
		byte[] b = strLine.getBytes("ISO-8859-1");//使用ISO-8859-1 對讀取到的內容進行解碼
		String str = new String(b,"UTF-8");//再使用UTF-8重新進行編碼
		System.out.println(str);
		//UTF-8 是修改版的UTF-8 會對讀取到的內容新增兩個位元組		
		System.out.println(raf.getFilePointer());
	}
}
package org.lanqiao.randomaccessfile.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo1 {
	public static void main(String[] args) throws IOException {
		//建立randomaccessfile的讀寫模式
		RandomAccessFile  rafOut = new RandomAccessFile("bb.txt","rw");
		//讀
		//RandomAccessFile rafIn  = new RandomAccessFile("bb.txt", "r");
		
		//準備寫出的資料:
		int  i = 10;//4
		double d = 11.12;//8
		boolean b = true;//1
		String as = "太";// 3 + 2
		
		String s = "太原師範學院";//18 + 2
		//寫
		rafOut.writeInt(i);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeDouble(d);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeBoolean(b);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeUTF(as);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeUTF(s);
		System.out.println("-----------" + rafOut.getFilePointer());
		//將指標移回到檔案的開始位置
		rafOut.seek(18);
		String str = "山西省太原市";
		rafOut.writeUTF(str);
		rafOut.seek(18);
		
		//讀
		/*int ii = rafOut.readInt();
		System.out.println(ii);
		double dd = rafOut.readDouble();
		System.out.println(dd);
		boolean bb = rafOut.readBoolean();
		System.out.println(bb);*/
		String ss = rafOut.readUTF();
		System.out.println(ss);
		
		
		//關閉
		rafOut.close();
		
		
	}
}
package org.lanqiao.randomaccessfile.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo3 {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("cc.txt", "rw");
		byte b = 2;
		int i = 255;// 00000000 00000000  00000000 10000001
		raf.writeByte(b);//每次寫一個位元組
		raf.writeByte(i);
		raf.seek(0);
		System.out.println(raf.readByte());//每次讀一個位元組
		System.out.println(raf.readByte());
		byte b1 = (byte) (i >>> 24);
		byte b2 = (byte) (i >>> 16);
		byte b3 = (byte) (i >>> 8);
		byte b4 = (byte) i;
		System.out.println(b1);
		System.out.println(b2);
		System.out.println(b3);
		System.out.println(b4);
	}
}