构造器: public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)
创建RandomAccessFile类实例需要指定一个mode参数,
该参数指定RandomAccessFile的访问模式:
r:以只读方式打开
rw: 读取和写入
rwd:读取写入 同步更新文件内容
rws:读取和写入 同步文件内容和元数据的更新
读取文件内容:
RandomAccessFile raf = new RandomAccessFile(“test.txt”, “rw”); raf.seek(5); byte [] b = new byte[1024]; int off = 0; int len = 5; raf.read(b, off, len); String str = new String(b, 0, len); System.out.println(str); raf.close();
写入文件内容:
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw"); raf.seek(5); //先读出来 String temp = raf.readLine(); raf.seek(5); raf.write("xyz".getBytes()); raf.write(temp.getBytes()); raf.close();
来源:https://www.cnblogs.com/afangfang/p/12610074.html