Java编写一个带缓存的对文本文件进行读写操作的程序。要求可以读取不同位置的信息,可以爸数据写入到不同的位置。创建一个学生类(含姓名,性别,年龄),从键盘输入学生信息,把学生对象保存在指定文本文件中的指定位置。从文本文件中读取特定学生信息。实现学生信息的增加,删除,修改和插入
时间: 2024-02-03 09:12:29 浏览: 78
JAVA实现文件操作(删除,创建,写入,读取)
5星 · 资源好评率100%
以下是一个实现带缓存的对文本文件进行读写操作的Java程序,并附带学生类的实现:
```java
import java.io.*;
class Student implements Serializable {
private String name;
private String gender;
private int age;
public Student(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
}
class FileBufferedReader {
private BufferedReader br;
private char[] buffer;
private int size;
private int pos;
public FileBufferedReader(String filename, int bufferSize) throws IOException {
br = new BufferedReader(new FileReader(filename));
buffer = new char[bufferSize];
size = br.read(buffer);
pos = 0;
}
public String readLine() throws IOException {
StringBuilder sb = new StringBuilder();
while (true) {
while (pos < size && buffer[pos] != '\n') {
sb.append(buffer[pos++]);
}
if (pos == size) {
size = br.read(buffer);
pos = 0;
if (size <= 0) {
break;
}
} else {
pos++;
break;
}
}
return sb.toString();
}
public void close() throws IOException {
br.close();
}
}
class FileBufferedWriter {
private BufferedWriter bw;
private char[] buffer;
private int size;
private int pos;
public FileBufferedWriter(String filename, int bufferSize) throws IOException {
bw = new BufferedWriter(new FileWriter(filename));
buffer = new char[bufferSize];
size = bufferSize;
pos = 0;
}
public void write(String s) throws IOException {
for (int i = 0; i < s.length(); i++) {
if (pos == size) {
bw.write(buffer, 0, size);
pos = 0;
}
buffer[pos++] = s.charAt(i);
}
}
public void flush() throws IOException {
if (pos > 0) {
bw.write(buffer, 0, pos);
pos = 0;
}
bw.flush();
}
public void close() throws IOException {
flush();
bw.close();
}
}
public class Main {
private static final int BUFFER_SIZE = 1024;
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 创建学生对象
Student student1 = new Student("张三", "男", 20);
Student student2 = new Student("李四", "女", 19);
Student student3 = new Student("王五", "男", 21);
// 将学生对象写入文件
FileBufferedWriter writer = new FileBufferedWriter("students.txt", BUFFER_SIZE);
writer.write(serialize(student1));
writer.write("\n");
writer.write(serialize(student2));
writer.write("\n");
writer.write(serialize(student3));
writer.flush();
writer.close();
// 从文件中读取学生对象
FileBufferedReader reader = new FileBufferedReader("students.txt", BUFFER_SIZE);
String line;
int pos = 0;
while ((line = reader.readLine()) != null) {
Student student = (Student) deserialize(line);
System.out.println("第" + (++pos) + "个学生:");
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getGender());
System.out.println("年龄:" + student.getAge());
System.out.println();
}
reader.close();
}
private static String serialize(Object obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
return new String(baos.toByteArray());
}
private static Object deserialize(String s) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}
}
```
以上程序中,`FileBufferedReader` 和 `FileBufferedWriter` 分别实现了带缓存的文件读取和写入操作。在 `main` 函数中,我们创建了三个学生对象,并将它们依次写入到 `students.txt` 文件中。接着,我们使用 `FileBufferedReader` 从文件中读取每个学生对象,并输出它们的信息。
如果需要实现学生信息的增加、删除、修改和插入操作,可以使用类似于上面的方式进行实现。例如,如果要增加一个学生对象,可以先读取整个文件,然后将新学生对象写入到文件末尾。如果要删除或修改一个学生对象,可以先读取整个文件,并找到需要操作的学生对象所在的位置,然后进行删除或修改操作。如果要插入一个学生对象,可以先将文件中插入点后面的内容读取出来,然后将新学生对象写入到文件中插入点的位置,最后将读取出来的内容写入到新学生对象后面。
阅读全文