12.2 RandomAccessFile类 (血干JAVA系列)
33/100
发布文章
qq_52384627
未选择文件
new
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指针位置
}
}
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 段落