IO的视频中
RandomAccessFile中
为什么读年龄不用指定读取的长度?
代码如下(红色标记)
package IOtest;
import java.io.*;
public class RandomAccessTest
{
public static void main(String [] args) throws Exception
{
Employee e1=new Employee("luokan",288);
Employee e2=new Employee("lujiayu",11);
Employee e3=new Employee("kkaasndias",23);
RandomAccessFile ra=new RandomAccessFile("f:\\1.txt","rw");
ra.write(e1.name.getBytes());
ra.writeInt(e1.age);
ra.write(e2.name.getBytes());
ra.writeInt(e2.age);
ra.write(e3.name.getBytes());
ra.writeInt(e3.age);
ra.close();
RandomAccessFile rb=new RandomAccessFile("f:\\1.txt","r");
rb.skipBytes(12);
String strName=null;
byte []buf=new byte[Employee.LEN];
int d=0;
d=rb.read(buf);
strName=new String(buf,0,Employee.LEN);
System.out.println(strName.trim()+":"+rb.read());
rb.seek(0);
d=rb.read(buf);
strName=new String(buf,0,Employee.LEN);
System.out.println(strName.trim()+":"+rb.read());
rb.skipBytes(12);
d=rb.read(buf);
strName=new String(buf,0,Employee.LEN);
System.out.println(strName+":"+rb.read());
rb.close();
}
} |