天天看點

Java IO RandomAccessFile 類随機通路流的使用細節詳解

Java IO RandomAccessFile 類随機通路流的使用細節詳解

    • 1. RandomAccessFile
    • 2.demo
      • 1.RandomAccessFile 實作讀和寫
      • 2.RandomAccessFile 實作插入效果
      • 2.RandomAccessFile 更為通用的插入實作

1. RandomAccessFile

  1. RandomAccessFile 類支援 “随機通路” 的方式,程式可以直接跳到檔案的任意地方來讀、寫檔案

    ① 支援隻通路檔案的部分内容

    ② 可以向已存在的檔案後追加内容

  2. RandomAccessFile 對象包含一個記錄指針,用以标示目前讀寫處的位置。RandomAccessFile 類對象可以自由移動記錄指針:

    ① long getFilePointer():擷取檔案記錄指針的目前位置

    ② void seek(long pos):将檔案記錄指針定位到 pos 位置

  3. 構造器

    ① public RandomAccessFile(File file, String mode)

    ② public RandomAccessFile(String name, String mode)

  4. 建立 RandomAccessFile 類執行個體需要指定一個 mode 參數,該參數指定 RandomAccessFile 的通路模式:

    ① r: 以隻讀方式打開

    ② rw:打開以便讀取和寫入

    ③ rwd:打開以便讀取和寫入;同步檔案内容的更新

    ④ rws:打開以便讀取和寫入;同步檔案内容和中繼資料的更新

2.demo

1.RandomAccessFile 實作讀和寫

//實際是實作覆寫的效果
	@Test
	public void test2() {
		RandomAccessFile raf  = null;
		try {
			raf = new RandomAccessFile(new File("hello1.txt"),"rw");
			raf.seek(3);//将指針調到3的位置
			raf.write("xy".getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(raf != null) {
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//進行檔案的讀、寫
	@Test
	public void test1() {
		RandomAccessFile raf1 = null;
		RandomAccessFile raf2 = null;
		try {
			raf1 = new RandomAccessFile(new File("hello.txt"),"r");
			raf2 = new RandomAccessFile(new File("hello1.txt"),"rw");
			
			byte [] b = new byte[20];
			int len ;
			while((len = raf1.read(b)) != -1) {
				raf2.write(b, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(raf2 != null) {
			try {
				raf2.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(raf1 != null){
			try {
				raf1.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
           

2.RandomAccessFile 實作插入效果

  1. 實作demo檔案hello.txt内容,第一個方法為
    Java IO RandomAccessFile 類随機通路流的使用細節詳解
    Java IO RandomAccessFile 類随機通路流的使用細節詳解
  2. 代碼
//實作插入的效果:在d字元的後面插入“xy”
	@Test
	public void test3() {
		RandomAccessFile raf  = null;
		try {
			raf = new RandomAccessFile(new File("hello.txt"),"rw");
			
			raf.seek(4);
			String str = raf.readLine();
//			long l = raf.getFilePointer();
//			System.out.println(l);//17
			raf.seek(4);
			raf.write("xy".getBytes());
			raf.write(str.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(raf != null) {
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
           

2.RandomAccessFile 更為通用的插入實作

  1. holle.txt檔案
    Java IO RandomAccessFile 類随機通路流的使用細節詳解
    Java IO RandomAccessFile 類随機通路流的使用細節詳解
  2. test4()寫法能夠對換行進行處理,效果更好,test3()的寫法,會導緻資料變化
  3. 代碼
//相較于test3,更通用	
	@Test
	public void test4() {
		RandomAccessFile raf  = null;
		try {
			raf = new RandomAccessFile(new File("hello.txt"),"rw");
			
			raf.seek(4);
			byte[] b = new byte[10];
			int len;
			StringBuffer sb = new StringBuffer();
			while((len = raf.read(b)) != -1) {
				sb.append(new String(b,0,len));
			}
			raf.seek(4);
			raf.write("xy".getBytes());
			raf.write(sb.toString().getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(raf != null) {
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}