package zxx4;
import java.io.*;
//首先定义一个类Employee,包含对字段格式的要求。
class Employee {
public String name=null;
public int age=0;
public static final int LEN= 8;
Employee(String name,int age){
if(name.length() > 8){
name = name.substring(0,LEN);
}
else {
while(name.length() < LEN){
name += '/u0000';
}
}
this.name = name;
this.age = age;
}
}
public class RandomFileTest{
public static void main(String[] args){
Employee e1 = new Employee("张三",238);
Employee e2 = new Employee("lisi",25);
Employee e3 = new Employee("wangwu",24);
try{ //文件操作需要捕获异常
RandomAccessFile ra = new RandomAccessFile("employee.txt","rw");
ra.writeChars(e1.name);
ra.writeInt(e1.age);
ra.writeChars(e2.name);
ra.writeInt(e2.age);
ra.writeChars(e3.name);
ra.writeInt(e3.age);
ra.close();
//int len = 0;
//byte[] buf = new byte[Employee.LEN];
String strName = "";
RandomAccessFile raf = new RandomAccessFile("employee.txt","r");
raf.skipBytes(Employee.LEN*2+4); //跳转到第二条记录
//len = raf.read(buf);
//strName = new String(buf,0,len);
strName = ""; //每次读取信息时需先将strName置空
for(int i = 0 ; i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim() + ":"+raf.readInt());
raf.seek(0);//绝对定位,跳转到头条记录
//len = raf.read(buf);
//strName = new String(buf,0,len);
strName = "";
for(int i = 0 ; i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim() + ":"+raf.readInt());
raf.skipBytes(Employee.LEN*2+4);//第一条记录读取完成时,指针停留在第二条记录一开始,这时需要再将指针跳过一条记录
//len = raf.read(buf);
//strName = new String(buf,0,len);
strName = "";
for(int i = 0 ; i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim() + ":"+raf.readInt());
raf.close(); //关闭文件
}catch(Exception e){
e.printStackTrace();
}
}
}
import java.io.*;
//首先定义一个类Employee,包含对字段格式的要求。
class Employee {
public String name=null;
public int age=0;
public static final int LEN= 8;
Employee(String name,int age){
if(name.length() > 8){
name = name.substring(0,LEN);
}
else {
while(name.length() < LEN){
name += '/u0000';
}
}
this.name = name;
this.age = age;
}
}
public class RandomFileTest{
public static void main(String[] args){
Employee e1 = new Employee("张三",238);
Employee e2 = new Employee("lisi",25);
Employee e3 = new Employee("wangwu",24);
try{ //文件操作需要捕获异常
RandomAccessFile ra = new RandomAccessFile("employee.txt","rw");
ra.writeChars(e1.name);
ra.writeInt(e1.age);
ra.writeChars(e2.name);
ra.writeInt(e2.age);
ra.writeChars(e3.name);
ra.writeInt(e3.age);
ra.close();
//int len = 0;
//byte[] buf = new byte[Employee.LEN];
String strName = "";
RandomAccessFile raf = new RandomAccessFile("employee.txt","r");
raf.skipBytes(Employee.LEN*2+4); //跳转到第二条记录
//len = raf.read(buf);
//strName = new String(buf,0,len);
strName = ""; //每次读取信息时需先将strName置空
for(int i = 0 ; i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim() + ":"+raf.readInt());
raf.seek(0);//绝对定位,跳转到头条记录
//len = raf.read(buf);
//strName = new String(buf,0,len);
strName = "";
for(int i = 0 ; i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim() + ":"+raf.readInt());
raf.skipBytes(Employee.LEN*2+4);//第一条记录读取完成时,指针停留在第二条记录一开始,这时需要再将指针跳过一条记录
//len = raf.read(buf);
//strName = new String(buf,0,len);
strName = "";
for(int i = 0 ; i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim() + ":"+raf.readInt());
raf.close(); //关闭文件
}catch(Exception e){
e.printStackTrace();
}
}
}