package com.yd.wmsc.util;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccesssFileTest {
public static void main(String[] args) {
RandomAccessFile r = null ;
try {
r = new RandomAccessFile("D:/heihei.txt", "rw");
r.writeChar('a');
System.out.println(r.getFilePointer());//2
r.writeChar('b');
r.writeChar('c');
System.out.println(r.getFilePointer());//6
//abc
r.seek(2);
r.writeChar('d'); //adc
StringBuffer strBuf = new StringBuffer();
r.seek(0); //指针归回初始位置
long length = r.length()/2;
System.out.println(length);
for (long i = 0L; i < length; i++) {
strBuf.append(r.readChar());
}
System.out.println(strBuf); //结果是adc
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}