国外java代码例子经典 国外java英文视频教程( 九 )


}
}
package IO;
import java.io.*;
public class FileRandomRW {
// 需要输入的person数目 。
public static int NUMBER = 3;
public static void main(String[] args) {
Persons[] people = new Persons[NUMBER];
people[0] = new Persons("张峰", 26, 2000, "N");
people[1] = new Persons("艳娜", 25, 50000, "Y");
people[2] = new Persons("李朋", 50, 7000, "F");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
"peoplerandom.dat"));
// 将人员数据保存至“peoplerandom.dat”二进制文件中 。
writeData(people, out);
// 关闭流 。
out.close();
// 从二进制文件“peoplerandom.dat”中逆序读取数据 。
RandomAccessFile inOut = new RandomAccessFile("peoplerandom.dat",
"rw");
Persons[] inPeople = readDataReverse(inOut);
// 输出读入的数据 。
System.out.println("原始数据:");
for (int i = 0; iinPeople.length; i++) {
System.out.println(inPeople[i]);
}
// 修改文件的第三条记录 。
inPeople[2].setSalary(4500);
// 将修改结果写入文件 。
inPeople[2].writeData(inOut, 3);
// 关闭流 。
inOut.close();
// 从文件中读入的第三条记录,并输出,以验证修改结果 。
RandomAccessFile in = new RandomAccessFile("peoplerandom.dat", "r");
Persons in3People = new Persons();
// 随机读第三条记录 。
in3People.readData(in, 3);
// 关闭流 。
in.close();
System.out.println("修改后的记录");
System.out.println(in3People);
} catch (IOException exception) {
System.err.println("IOException");
}
}
// 将数据写入输出流 。
static void writeData(Persons[] p, DataOutputStream out) throws IOException {
for (int i = 0; ip.length; i++) {
p[i].writeData(out);
}
}
// 将数据从输入流中逆序读出 。
static Persons[] readDataReverse(RandomAccessFile in) throws IOException {
// 获得记录数目 。
int record_num = (int) (in.length() / Persons.RECORD_LENGTH);
Persons[] p = new Persons[record_num];
// 逆序读取 。
for (int i = record_num - 1; i = 0; i--) {
p[i] = new Persons();
// 文件定位 。
in.seek(i * Persons.RECORD_LENGTH);
p[i].readData(in, i + 1);
}
return p;
}
}
class Persons {
private String name;
private int age; // 4个字节
private double salary; // 8个字节
private String married;
public static final int NAME_LENGTH = 20; // 姓名长度
public static final int MARRIED_LENGTH = 2; // 婚否长度
public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8
+ MARRIED_LENGTH * 2;
public Persons() {
}
public Persons(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = "F";
}
public Persons(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
public String setName(String n) {
name = n;
return name;
}
public int setAge(int a) {
age = a;
return age;
}
public double setSalary(double s) {
salary = s;
return salary;
}
public String setMarried(String m) {
married = m;
return married;
}
// 设置输出格式 。
public String toString() {
return getClass().getName() + "[name=" + name + ",age=" + age
+ ",salary=" + salary + ",married=" + married + "]";
}
// 写入一条固定长度的记录,即一个人的数据到输出流 。

推荐阅读