一、RandomAccessFile 介绍
1. 直接看RandomAccessFile的源码 ,常用的实例方法有:
1) 传入文件名和访问文件的模式,mode为访问文件的权限
public RandomAccessFile(String name, String mode)
throws FileNotFoundException
{
this(name != null ? new File(name) : null, mode);
}
2) 传入文件对象和权限 ,在定义文件的时候,定义了一些用了标志访问权限的常量。
private static final int O_RDONLY = 1;
private static final int O_RDWR = 2;
private static final int O_SYNC = 4;
private static final int O_DSYNC = 8;
public RandomAccessFile(File file, String mode)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
int imode = -1;
if (mode.equals("r"))
imode = O_RDONLY; // 1
else if (mode.startsWith("rw")) {
imode = O_RDWR;
rw = true;
if (mode.length() > 2) {
if (mode.equals("rws"))
imode |= O_SYNC; //4
else if (mode.equals("rwd"))
imode |= O_DSYNC; //8
else
imode = -1;
}
}
if (imode < 0)
throw new IllegalArgumentException("Illegal mode \"" + mode
+ "\" must be one of "
+ "\"r\", \"rw\", \"rws\","
+ " or \"rwd\"");
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
if (rw) {
security.checkWrite(name);
}
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.attach(this);
path = name;
open(name, imode);
}
3)参数mode介绍
r : 表示该文件只读。
rw: 表示该文件可读可写。
rws: 表示该文件支持同步的可读可写。
rwd: 表示该文件不支持同步的可读可写。
2. RandomAccessFile 类的常用方法介绍
1) public int read(byte b[]) ; read方法将文件以字节流的形式读取出来并以字节数组来存取。
2) public native long getFilePointer() throws IOException; getFilePoint方法用来读取文件指针现在所在文件的哪个位置。
3) public void seek(long pos) throws IOException; seek方法可以将文件指针移动到指定的位置。
4) public native long length() throws IOException; length方法获取文件的总长度。
3. 案例使用
将登录的用户名和密码用.dat文件来存储,用户名和密码的长度为32字节,.dat文件每行100字节。
1) 注册
/*
* 将注册信息写入到user.dat文件中
* 每条记录占用100字节,其中,用户名,密码,昵称为字符串
* 各占用32字节,年龄为int值占用4个字节
*
*/
try (RandomAccessFile raf=new RandomAccessFile("user.dat","rw");){
raf.seek(raf.length());
byte[]data=username.getBytes("UTF-8");
data=Arrays.copyOf(data, 32);
raf.write(data);//将数据写入到文件中
data=password.getBytes("UTF-8");
data=Arrays.copyOf(data, 32);
raf.write(data);
data=nickname.getBytes("UTF-8");
data=Arrays.copyOf(data, 32);
raf.write(data);
data=request.getParameter("age").getBytes("UTF-8");
data=Arrays.copyOf(data, 4);
raf.write(data);
} catch (Exception e) {
e.printStackTrace();
}
2)登录
读取.dat文件,取出用户名和密码与传入的用户名和密码进行匹配,逐行读取,如果读取不到,将指针移动下一行。
try (RandomAccessFile raf=new RandomAccessFile("user.dat","r");){
String username=request.getParameter("username");
System.out.println("username:"+username);
String password=request.getParameter("password");
System.out.println("password:"+password);
boolean flag=false;
for(int i=0;i<raf.length()/100;i++) {
//读取第0-32个字节和32-64个字节与username 和password进行比较
byte[]data=new byte[32];
raf.read(data);
String user_name=new String(data,"utf-8").trim();
raf.read(data);
String pass_word=new String(data,"utf-8").trim();
if(username!=null&&password!=null) {
if(username.equals(user_name)&&password.equals(pass_word)) {
//登录成功
flag=true;
response.setEntity(new File("webapps/myweb/login_success.html"));
System.out.println("登录成功!");
break;
}else {
raf.seek(100+100*i);
continue;
}
}else {
System.out.println("用户名和密码为空!");
}
}
if(!flag) {
response.setEntity(new File("webapps/myweb/login_fail.html"));
System.out.println("对不起,登录失败!输入不正确!");
}
} catch (Exception e) {
e.printStackTrace();
}