package randomaccessfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
//文件的随机访问
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
File f = new File("process/random.txt");
//writeOut(f);
readIn(f);
}
private static void writeOut(File f) throws Exception {
RandomAccessFile rf = new RandomAccessFile(f,"rw");
rf.writeByte(126);
rf.writeBoolean(true);
rf.writeChar('s');
rf.writeUTF("蜀道难");
rf.close();
}
private static void readIn(File f) throws Exception {
RandomAccessFile rf = new RandomAccessFile(f,"r");
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readByte());
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readBoolean());
System.out.println("指针位置:"+rf.getFilePointer());
rf.seek(0);
System.out.println("指针位置:"+rf.getFilePointer());
rf.seek(2);
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readChar());
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readUTF());
System.out.println("指针位置:"+rf.getFilePointer());
rf.close();
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
//文件的随机访问
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
File f = new File("process/random.txt");
//writeOut(f);
readIn(f);
}
private static void writeOut(File f) throws Exception {
RandomAccessFile rf = new RandomAccessFile(f,"rw");
rf.writeByte(126);
rf.writeBoolean(true);
rf.writeChar('s');
rf.writeUTF("蜀道难");
rf.close();
}
private static void readIn(File f) throws Exception {
RandomAccessFile rf = new RandomAccessFile(f,"r");
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readByte());
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readBoolean());
System.out.println("指针位置:"+rf.getFilePointer());
rf.seek(0);
System.out.println("指针位置:"+rf.getFilePointer());
rf.seek(2);
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readChar());
System.out.println("指针位置:"+rf.getFilePointer());
System.out.println(rf.readUTF());
System.out.println("指针位置:"+rf.getFilePointer());
rf.close();
}
}