RandomAccessFile进行随机读写文件:
package shangwu;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
//随机访问文件(移动指针到任意位置)RandomAccessFile
//既可以读也可以写DateOutput,DateInput,Closeable;
public class B1 {
public static void main(String[] args) throws IOException {
try {
//1创建RandomAccessFile类对象——>(文件不存在会自动创建一个)
File file = new File("src/stu.txt");
RandomAccessFile raf=new RandomAccessFile(file, "rwd");//rwd代表可读可写持续刷新
System.out.println("文件指针位置:"+raf.getFilePointer());
System.out.println("文件字节大小"+file.length());
raf.writeInt(1);//加入一个1
raf.seek(0);//设置文本指针的位置->指针指向0:
System.out.println("2文件指针位置:"+raf.getFilePointer());
System.out.println("2文件字节大小"+file.length());
raf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}