总述:一个既可以读也可以写的操作,专门用来读取文件数据,其他的建议用流操作
Instances of this class support both reading and writing to a random access file.
void | close() Closes this random access file stream and releases any system resources associated with the stream. |
关闭操作,释放内存,在C里面特别严重,Java的底层是C,所以必须关
long | length() Returns the length of this file. |
文件的长度
void | seek(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. |
很讨喜的一个东西,是RAF的指针,控制他的位置,灵活运用后高效读写
void | write(int b) Writes the specified byte to this file. |
写操作
public class WriteStringDemo04 { public static void main(String[] args) throws IOException { RandomAccessFile raf =new RandomAccessFile("raf.txt","rw"); String str = "你在南方的艳阳里,大雪纷飞。"; /* * String提供了将字符串转换为字节的方法: * byte[] getBytes() * 按照系统默认字符集转换(不推荐,存在平台差异) * * byte[] getBytes(String csn) * 按照给定的字符集转换,字符集的名字不区分大小写 * 常见字符集: * GDK:国标编码 * UTF-8:unicode的子集,也成为万国码 * ISO8859-1:欧洲字符集,不支持中文 */ byte[] data = str.getBytes("utf-8"); raf.write(data); System.out.println("写出完毕!"); raf.close(); } }
int | read() Reads a byte of data from this file. |
读操作
public class RAF_read02 { public static void main(String[] args) throws IOException { /* * “./”可以忽略,不写默认也是当前目录中 */ RandomAccessFile raf = new RandomAccessFile("raf.dat","r"); /* * int read() * 从文件中写入1字节数据,并以int形式返回。 * 若返回值为-1则表示读取到了文件末尾。 * * 00000000 00000000 00000000 00000001 */ int d = raf.read(); System.out.println(d); d = raf.read(); System.out.println(d); raf.close(); } }
读写一起操作,可以实现复制的功能
import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Scanner; /** * 修改密码功能 * 程序启动后,要求用户输入用户名及密码,先确认旧密码,然后将user.dat文件中该用户的密码更改。 * 若没有该用户,则提示:用户名无效。 * @author soft01 * */ public class Test5 { public static void main(String[] args) throws UnsupportedEncodingException, IOException { RandomAccessFile raf =new RandomAccessFile("user.dat","rw"); //先将指针移动到文件末尾,以便追加新纪录 Scanner scan = new Scanner(System.in); System.out.println("请输入您要查询的名字"); String name = scan.nextLine(); String newPassword; int index = 0; int num = 0; for (int i = 0; i < raf.length()/100; i++) { raf.seek(index); //读用户名 byte[] data1 = new byte[32]; raf.read(data1);//指针在32 String name1 = new String(data1,"UTF-8").trim(); if(name.equals(name1)){ num +=1;//名字相同时计数加一 System.out.println("please input old password:"); String oldPassword = scan.nextLine(); raf.read(data1);//指针在64 String pwd = new String(data1,"UTF-8").trim(); if(oldPassword.equals(pwd)){ raf.seek(i*100+32); System.out.println("please input new password:"); newPassword = scan.nextLine(); byte[] data = newPassword.getBytes("UTF-8"); data = Arrays.copyOf(data, 32); raf.write(data);//指针在64 }else{ System.out.println("you oldPassword input is wrong"); break; } //System.out.println(raf.getFilePointer()); //System.out.println(raf.getFilePointer()); raf.seek(i*100+32); raf.read(data1); newPassword = new String(data1,"UTF-8").trim(); //System.out.println(raf.getFilePointer()); //读昵称 raf.read(data1); String nickname1 = new String(data1,"UTF-8").trim(); //读年龄 int age1 = raf.readInt(); System.out.println("修改后的用户名和密码:"+name1+","+newPassword+","+nickname1+","+age1); raf.seek(i*100); } //RAF指针 index += 100; } if(num == 0){ System.out.println("用户名无效"); } raf.close(); } }