package stream.demo1;
import java.io.IOException;
import java.io.RandomAccessFile;
/** 文件操作类,游离于IO树形结构外,类似DataInputStream和DataOutputStream 的集成类 */
public class UsingRandomAccessFile
{
static String file = "rtest.dat";
static void display()
throws IOException
{
RandomAccessFile rf = new RandomAccessFile(file, "r");
for (int i = 0; i < 7; i++)
System.out.println("Value " + i + " : " + rf.readDouble());
System.out.println(rf.readUTF());
rf.close();
}
public static void main(String[] args)
throws IOException
{
// RandomAccessFile适用于由大小已知的记录组成的文件,可以用seek()将记录从一处转移到另一处
RandomAccessFile rf = new RandomAccessFile(file, "rw");
for (int i = 0; i < 7; i++)
rf.writeDouble(i * 1.414);
rf.writeUTF("the end of the file");
rf.close();
display();
rf = new RandomAccessFile(file, "rw");
rf.seek(5 * 8);// 偏移40个直接,不包含空格
rf.writeDouble(47.0001);
rf.close();
display();
}
}
io-stream UsingRandomAccessFile
最新推荐文章于 2018-11-14 23:55:27 发布