1.RandomAccessFile基于指针的读写形式
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAFDemo3 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
long pos = raf.getFilePointer();
int max = Integer.MAX_VALUE;
double a = 123.12;
long b = 123L;
raf.writeInt(max);
System.out.println("当前指针位置:"+raf.getFilePointer());
raf.writeDouble(a);
System.out.println("当前指针位置:"+raf.getFilePointer());
raf.writeLong(b);
System.out.println("当前指针位置:"+raf.getFilePointer());
System.out.println("读写完毕");
raf.seek(4);
double d = raf.readDouble();
System.out.println(d);
long e = raf.readLong();
System.out.println(e);
System.out.println("当前指针位置:"+raf.getFilePointer());
raf.seek(0);
long l = raf.readLong();
System.out.println(l);
raf.seek(12);
raf.writeLong(123456789L);
System.out.println("long值修改完毕!");
raf.seek(12);
long x = raf.readLong();
System.out.println(x);
raf.close();
}
}
2.读取字符串
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadStringDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile scr = new RandomAccessFile("fos.txt","r");
byte[] data = new byte[(int) scr.length()];
scr.read(data);
String str = new String(data,"UTF-8");
System.out.println(str);
scr.close();
}
}
3.RandomAccessFile实现注册功能
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
public class RegDemo {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("请输入用户名称:");
String username = scan.nextLine();
System.out.println("请输入密码:");
String password = scan.next();
System.out.println("请输入昵称:");
String nickName = scan.next();
System.out.println("请输入年龄:");
int age = scan.nextInt();
RandomAccessFile raf = new RandomAccessFile("raf.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);
raf.writeInt(age);
System.out.println("注册完毕!");
raf.close();
}
}
4.显示所有用户
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ShowAllUserDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
for(int i=0;i<raf.length()/100;i++) {
byte[] data = new byte[32];
raf.read(data);
String username = new String(data, "UTF-8").trim();
raf.read(data);
String password = new String(data, "UTF-8").trim();
raf.read(data);
String nickName = new String(data, "UTF-8").trim();
int age = raf.readInt();
System.out.println(username + "," + password + "," + nickName + "," + age);
raf.close();
}
}
}
5.写字符串
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
public class WriteStringDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
for(int i=0;i<26;i++){
raf.write('a'+i);
}
String str = "我爱学习我的祖国";
byte[] data = str.getBytes("UTF-8");
raf.write(data);
System.out.println("写出完毕!");
raf.close();
}
}