RandomAccessFile

import java.io.*;


/*
随机读写访问,最好是对规律的数据进行操作,
该类既可以对数据进行读取,也可以写入。通过构造函数中的模式来进行区分。

它最具有特色的方法:seek():确定指针的位置,
					skipBytes():跳过多少字节数。
					getFilePointer():获取指针的位置。

*/
class  RandomAccessFileDemo
{
	public static void main(String[] args) throws IOException
	{
		
		RandomAccessFile raf = new RandomAccessFile("info.txt","rw");
		/*
		raf.seek(8);
		raf.write("李四".getBytes());
		raf.writeInt(97);
		raf.seek(8*3);
		raf.write("周七".getBytes());
		raf.writeInt(106);

		*/

		raf.seek(0);
		raf.write("张三".getBytes());
		raf.writeInt(108);
		raf.seek(8*2);
		raf.write("张八".getBytes());
		raf.writeInt(110);
		/*

		RandomAccessFile raf = new RandomAccessFile("info.txt","r");

//		raf.skipBytes(8);

		raf.seek(8);

		byte[] buf = new byte[4];

		raf.read(buf);

		String name = new String(buf);

		System.out.println(name);

		int age = raf.readInt();
		System.out.println(age);
		raf.close();*/


	}
}
/*
练习:在两个信息中插入信息。(结果出现数据覆盖现象,所以需要预留数据空间位置,)
*/

 

你可能感兴趣的:(RandomAccessFile)