跳跃式读写文件

package demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {

	public static void main(String[] args) throws Exception {
		//跳跃式读写文件
		File file=new File("D:"+File.separator+"abc.txt");
		RandomAccessFile raf=new RandomAccessFile(file,"rw");		//读写模式
		String names[]=new String[] {"zhangsan","lisi","wangwu"};	
		int ages[]=new int[] {10,20,30};
		for(int i=0;i<3;i++) {
			raf.write(names[i].getBytes());
			raf.writeInt(ages[i]);
		}
		
		raf.seek(0);		//跳转到开头
		byte[] buf=new byte[8];
		int len=raf.read(buf);
		System.out.println("姓名:"+new String(buf,0,len));
		raf.skipBytes(10);		//跳过10个字节
		len=raf.read(buf);
		System.out.println("姓名:"+new String(buf,0,len));
		
		raf.close();
	}

}

posted @   fighterk  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示