随机访问文件的读取和写入对象:RandomAccessFileDemo

RandomAccessFileDemo对象:

 

 

 

 

import java.io.*;
class RandomAccessFileDemo
{
	public static void main(String[] args) throws IOException
	{
		//writeFile();
		readFile();
	}
	
	public static void readFile() throws IOException
	{
		RandomAccessFile raf = new RandomAccessFile("RandomAccessDemo.txt","r");
		
		//调整对象中的指针
		raf.seek(8*0);
		
		//跳过指定的字节数
		raf.skipBytes(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);
	}
	
	public static void writeFile() throws IOException
	{
		RandomAccessFile raf = new RandomAccessFile("RandomAccessDemo.txt","rw");
		
		raf.write("李四".getBytes());
		raf.writeInt(97);
		raf.write("王五".getBytes());
		raf.writeInt(99);
		
		raf.close();
	}
}


 

你可能感兴趣的:(java,IO,对象)