package cn.stat.p2.demo;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.randomaccessfile;
public class randomaccessfiledemo {
/**
* @param args
* @throws ioexception
*/
public static void main(string[] args) throws ioexception {
// todo auto-generated method stub
radomwrite();
radomread();
}
public static void radomread() throws filenotfoundexception, ioexception {
randomaccessfile raf=new randomaccessfile("demo.txt","rw");
byte[] buf=new byte[4];
raf.read(buf);
string name=new string(buf);
int age=raf.readint();
system.out.println("name="+name);
system.out.println("age="+age);
//获取指针位置
system.out.println("当前位置="+raf.getfilepointer());
//设定指针位置
raf.seek(1*12);
system.out.println("age="+raf.readint());
raf.close();
}
public static void radomwrite() throws filenotfoundexception, ioexception {
randomaccessfile raf=new randomaccessfile("demo.txt","rw");
raf.write("张三".getbytes());
raf.writeint(97);
raf.write("赵六".getbytes());
raf.writeint(98);
raf.close();
}
}
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!