12.2 RandomAccessFile类 (血干JAVA系列)

12.2 RandomAccessFile类 (血干JAVA系列)
33/100
发布文章
qq_52384627
未选择文件
new

在这里插入图片描述

RandomAccessFile类常用方法

序号   方法或常量   类型    描 述
 
1 public RandomAccess File (File file, String mode) throws FileNotFoundException 构造 接收File类的对象,指定操作路径,但是在设置时需要设置模式,“r” 为 只 读 , “ w ” 为 只 写 ,“rw” 为读写
2 public RandomAccessFile (String name, String mode) throws FileNotFoundException 构造 不再使用File类对象表示文件,而是直接输入了一个固定的文件路径
3 public void close() throws IOException 普通 关闭操作
4 public int read (byte[] b) throws IOException 普通 将内容读取到一个byte数组之中
5 public final byte readByte() throws IOException 普通 读取一个字节
6 public final int readlnt() throws IOException 普通 从文件中读取整型数据
7 public void seek(long pos) throws IOException 普通 设置读指针的位置
8 public final void writeBytes (String s) throws IOException 普通 将一个字符串写入到文件之中,按字节的方式处理
9 public final void writelnt (int v) throws IOException普通 将一个int型数据写入文件,长度为410 public int skipBytes (int n) throws IOException 普通 指针跳过多少个字节

12.2.1 使用 RandomAccessFile 类写入数据

写入的名字都必须是8个字节,
写入的数字是固定的4个字节

package jiaqi;

import java.io.File;

import java.io.RandomAccessFile;

public class demo377_1
{

	public static void main(String[] args) throws Exception//抛出异常,不处理
	{
		File f=new File("d:" + File.separator + "test.txt");
		//构造函数会抛出异常
		//rw打开文件
		RandomAccessFile rdf=new RandomAccessFile(f, "rw");
		
		String name=null;
		int age=0;
		
		name="zhangsan";
		age=30;
		//字符串读入,按字节处理
		rdf.writeBytes(name);
		rdf.writeInt(age);
		
		name="lisi    ";
		age=31;
		rdf.writeBytes(name);
		rdf.writeInt(age);
		
		name="wangwu  ";
		age=32;
		rdf.writeBytes(name);
		rdf.writeInt(age);
	    //关闭文件
		rdf.close();
	}
}

12.2.2 使用 RandomAccessFile 类读取数据

package jiaqi;

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

public class demo378_1 
{

	public static void main(String[] args) throws Exception 
	{
		File f=new File("d:" + File.separator + "test.txt");
		RandomAccessFile rdf=new RandomAccessFile(f, "r");
		
		String name=null;
		int age=0;
		byte b[]=new byte[8];
		
		//  2      1     3
		//11-23  0-11  23-35指针位置
		
		//0指针位置
		rdf.skipBytes(12);
		//11指针位置
		for(int i=0;i<b.length;i++)
		{
			b[i]=rdf.readByte();
		}
		name=new String(b);
		age=rdf.readInt();
		System.out.println("第2个人的信息=姓名:" + name + ";年龄"+ age);
		//23指针位置
		
		rdf.seek(0);
		//0指针位置
		for(int i=0;i<b.length;i++)
		{
			b[i]=rdf.readByte();
		}
		name=new String(b);
		age=rdf.readInt();
		System.out.println("第1个人的信息=姓名:" + name + ";年龄"+ age);
		//11指针位置
		
		rdf.skipBytes(12);
		//23指针位置
		for(int i=0;i<b.length;i++)
		{
			b[i]=rdf.readByte();
		}
		name=new String(b);
		age=rdf.readInt();	
		System.out.println("第3个人的信息=姓名:" + name + ";年龄"+ age);
		//35指针位置
		
	}

}

RandomAccessFile类
RandomAccessFile类常用方法
12.2.1 使用 RandomAccessFile 类写入数据
12.2.2 使用 RandomAccessFile 类读取数据
在这里插入图片描述

RandomAccessFile类常用方法
序号 方法或常量 类型 描 述

1 public RandomAccess File (File file, String mode) throws FileNotFoundException 构造 接收File类的对象,指定操作路径,但是在设置时需要设置模式,“r” 为 只 读 , “ w ” 为 只 写 ,“rw” 为读写
2 public RandomAccessFile (String name, String mode) throws FileNotFoundException 构造 不再使用File类对象表示文件,而是直接输入了一个固定的文件路径
3 public void close() throws IOException 普通 关闭操作
4 public int read (byte[] b) throws IOException 普通 将内容读取到一个byte数组之中
5 public final byte readByte() throws IOException 普通 读取一个字节
6 public final int readlnt() throws IOException 普通 从文件中读取整型数据
7 public void seek(long pos) throws IOException 普通 设置读指针的位置
8 public final void writeBytes (String s) throws IOException 普通 将一个字符串写入到文件之中,按字节的方式处理
9 public final void writelnt (int v) throws IOException普通 将一个int型数据写入文件,长度为4位
10 public int skipBytes (int n) throws IOException 普通 指针跳过多少个字节
12.2.1 使用 RandomAccessFile 类写入数据
写入的名字都必须是8个字节,
写入的数字是固定的4个字节

package jiaqi;

import java.io.File;

import java.io.RandomAccessFile;

public class demo377_1
{

public static void main(String[] args) throws Exception//抛出异常,不处理
{
	File f=new File("d:" + File.separator + "test.txt");
	//构造函数会抛出异常
	//rw打开文件
	RandomAccessFile rdf=new RandomAccessFile(f, "rw");
	
	String name=null;
	int age=0;
	
	name="zhangsan";
	age=30;
	//字符串读入,按字节处理
	rdf.writeBytes(name);
	rdf.writeInt(age);
	
	name="lisi    ";
	age=31;
	rdf.writeBytes(name);
	rdf.writeInt(age);
	
	name="wangwu  ";
	age=32;
	rdf.writeBytes(name);
	rdf.writeInt(age);
    //关闭文件
	rdf.close();
}

}

12.2.2 使用 RandomAccessFile 类读取数据
package jiaqi;

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

public class demo378_1
{

public static void main(String[] args) throws Exception 
{
	File f=new File("d:" + File.separator + "test.txt");
	RandomAccessFile rdf=new RandomAccessFile(f, "r");
	
	String name=null;
	int age=0;
	byte b[]=new byte[8];
	
	//  2      1     3
	//11-23  0-11  23-35指针位置
	
	//0指针位置
	rdf.skipBytes(12);
	//11指针位置
	for(int i=0;i<b.length;i++)
	{
		b[i]=rdf.readByte();
	}
	name=new String(b);
	age=rdf.readInt();
	System.out.println("第2个人的信息=姓名:" + name + ";年龄"+ age);
	//23指针位置
	
	rdf.seek(0);
	//0指针位置
	for(int i=0;i<b.length;i++)
	{
		b[i]=rdf.readByte();
	}
	name=new String(b);
	age=rdf.readInt();
	System.out.println("第1个人的信息=姓名:" + name + ";年龄"+ age);
	//11指针位置
	
	rdf.skipBytes(12);
	//23指针位置
	for(int i=0;i<b.length;i++)
	{
		b[i]=rdf.readByte();
	}
	name=new String(b);
	age=rdf.readInt();	
	System.out.println("第3个人的信息=姓名:" + name + ";年龄"+ age);
	//35指针位置
	
}

}

Markdown 2234 字数 126 行数 当前行 1, 当前列 0HTML 2191 字数 94 段落

  • 26
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯卡码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值