educoder实验六第二题
程序员文章站
2022-07-10 18:03:10
public class Student { public char[] name = {'\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000'}; public int age; public Student() { } public Student(String name, int age) { if (name.leng...
public class Student {
public char[] name = {'\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000'};
public int age;
public Student() {
}
public Student(String name, int age) {
if (name.length() > 10) {
System.arraycopy(name.toCharArray(), 0, this.name, 0, 10);
} else {
System.arraycopy(name.toCharArray(), 0, this.name, 0, name.toCharArray().length);
}
this.age = age;
}
public void Print() {
System.out.println(String.valueOf(name) + " " + age );
}
@Override
public String toString() {
return String.valueOf(name) + " " + age ;
}
}
public class RWClass {
private String filename;
public RWClass(String filename) {
this.filename = filename;
}
public void saveStudent(Student student, int pos) throws Exception {
RandomAccessFile rw = null;
rw = new RandomAccessFile(filename, "rw");
rw.seek(pos * 28);
for (int i = 0; i < 10; i++) {
rw.writeChar(student.name[i]);
}
rw.writeInt(student.age);
rw.close();
}
public Student readStudent(int pos) throws Exception {
char[] read = new char[10];
RandomAccessFile r = new RandomAccessFile(filename, "r");
r.seek(pos * 28);
for (int i = 0; i < 10; i++) {
read[i] = r.readChar();
}
int age = r.readInt();
return new Student(read.toString(),age);
}
}
本文地址:https://blog.csdn.net/qq_46197019/article/details/110292883