编写一个带缓存的对文本文件进行读写操作的程序。要求可以读取不同位置的信息,可以爸数据写入到不同的位置。创建一个学生类(含姓名,性别,年龄),从键盘输入学生信息,把学生对象保存在指定文本文件中的指定位置。从文本文件中读取特定学生信息。实现学生信息的增加,删除,修改和插入
时间: 2024-02-03 11:12:29 浏览: 81
以下是一个简单的带缓存的文本文件读写程序的示例:
```python
class FileCache:
def __init__(self, filename, block_size=1024):
self.filename = filename
self.block_size = block_size
self.cache = {}
def read(self, pos, size):
block_start = pos // self.block_size * self.block_size
block_end = (pos + size - 1) // self.block_size * self.block_size
data = b""
for block_pos in range(block_start, block_end + 1, self.block_size):
if block_pos in self.cache:
block_data = self.cache[block_pos]
else:
with open(self.filename, "rb") as f:
f.seek(block_pos)
block_data = f.read(self.block_size)
self.cache[block_pos] = block_data
data += block_data[pos - block_pos : pos - block_pos + size]
return data
def write(self, pos, data):
block_start = pos // self.block_size * self.block_size
block_end = (pos + len(data) - 1) // self.block_size * self.block_size
for block_pos in range(block_start, block_end + 1, self.block_size):
block_data = self.cache.get(block_pos, b"\x00" * self.block_size)
block_data = block_data[: pos - block_pos] + data[: self.block_size - (pos - block_pos)] + block_data[
pos - block_pos + len(data) :
]
self.cache[block_pos] = block_data
with open(self.filename, "r+b") as f:
f.seek(block_pos)
f.write(block_data)
data = data[self.block_size - (pos - block_pos) :]
class Student:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def serialize(self):
return f"{self.name},{self.gender},{self.age}\n".encode()
@staticmethod
def deserialize(data):
name, gender, age = data.decode().strip().split(",")
return Student(name, gender, int(age))
class StudentDB:
def __init__(self, filename):
self.filename = filename
self.cache = FileCache(filename)
def add_student(self, student):
pos = self.cache.write(len(self.cache.read(0, 0)), student.serialize())
return pos
def get_student(self, pos):
data = self.cache.read(pos, 1024)
return Student.deserialize(data)
def delete_student(self, pos):
data = self.cache.read(pos, 1024)
student = Student.deserialize(data)
self.cache.write(pos, b"\x00" * len(data))
return student
def update_student(self, pos, student):
data = student.serialize()
self.cache.write(pos, data)
def insert_student(self, pos, student):
data = self.cache.read(pos, 1024)
self.cache.write(pos + len(data), data)
self.cache.write(pos, student.serialize())
```
使用示例:
```python
db = StudentDB("students.txt")
# 添加学生
s1 = Student("张三", "男", 18)
s1_pos = db.add_student(s1)
# 获取学生
s2 = db.get_student(s1_pos)
print(s2.name, s2.gender, s2.age)
# 修改学生
s1.age = 19
db.update_student(s1_pos, s1)
s2 = db.get_student(s1_pos)
print(s2.name, s2.gender, s2.age)
# 插入学生
s3 = Student("李四", "女", 20)
s3_pos = db.add_student(s3)
db.insert_student(s1_pos, s3)
s2 = db.get_student(s1_pos)
print(s2.name, s2.gender, s2.age)
s4 = db.get_student(s3_pos)
print(s4.name, s4.gender, s4.age)
# 删除学生
db.delete_student(s1_pos)
```
阅读全文