package _9RandomAccessFile类;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
// 流的方式
// 写入数据,按照写入数据的顺序存储
// 读出的时候,顺序读出,比如你要读第100个数据,则需要先读前99个
// 流
// RandomAccessFile 随机存取文件任意位置数据
public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 1. 写入
// testWrite();
// 2. 读出
// testRead();
// 3. 修改
testModify();
}
private static void testModify() throws IOException {
// TODO Auto-generated method stub
RandomAccessFile f = new RandomAccessFile(new File("e:\\igeek\\datax1.txt"), "rw");
f.seek(5);// point挪到位置5
f.writeDouble(4.1415926);
f.close();
}
private static void testRead() throws IOException
{
RandomAccessFile f = new RandomAccessFile(new File("e:\\igeek\\datax1.txt"), "r");
// System.out.println(f.readBoolean());
// System.out.println(f.getFilePointer());// 得到当前偏移的位置 1
// System.out.println(f.readInt());
// System.out.println(f.getFilePointer());// 5
// System.out.println(f.readDouble());
// System.out.println(f.getFilePointer());// 13
// System.out.println(f.readUTF());
// System.out.println(f.getFilePointer());//
f.seek(5);// point挪到5位置
System.out.println(f.readDouble());
f.close();
}
private static void testWrite() throws IOException {
// TODO Auto-generated method stub
// 1. 打开文件
RandomAccessFile f = new RandomAccessFile(new File("e:\\igeek\\datax1.txt"), "rw");
// 2. 写入信息
f.writeBoolean(true); // 1/4
f.writeInt(10); // 4
f.writeDouble(3.1415926); // 8
f.writeUTF("张三李四王五"); // 3*6 =18
// 3. 关闭文件
f.close();
}
}