class RandomAccessFileDemo
{
public static void main(String [] args) throws IOException
{
//writeFile();
readFile();
}
public static void readFile() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","r");//只读
//raf.write("haha".getBytes());
raf.seek(8);//加了这行代码,调整指针后,发生报错异常,什么原因?该怎么调整?如果不加这行代码,age显示的却不是年龄,也不知道什么原因?
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
//System.out.println("name"+name);
int age =raf.readInt();
System.out.println("name:"+name);
System.out.println("age:"+age);
raf.close();
}
public static void writeFile()throws IOException
{
RandomAccessFile raf = new RandomAccessFile