RandomAccessFile的使用方法

来源:互联网 发布:sql语言实训 编辑:程序博客网 时间:2024/05/16 11:06

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

public class TestRandomAccessFile {
 public static void main(String[] args) throws FileNotFoundException {

  try{
   RandomAccessFile rf = new RandomAccessFile("random.txt","rw");
   rf.writeBoolean(true);
   rf.writeInt(123456);
   rf.writeChar('j');
   rf.writeDouble(1234.56);
   rf.seek(1);  //文件指针跳转到1位置
   System.out.println(rf.readInt());
   System.out.println(rf.readChar());
   System.out.println(rf.readDouble());
   rf.seek(0);
   System.out.println(rf.readBoolean());
   rf.close();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

原创粉丝点击