将数据存储到文件中和读取

android 的Context提供了openFileoutput()方法,所以我们可以借助这个方法来获取一个文件输出流(FileoutputSteam)对象,
MODE_APPEND 是写入的时候都在文件末尾写入,
MODE_PRIVATE 每次写入都会覆盖掉所有数据。
FileOutputStream outputStream = openFileOutput( "data" , MODE_APPEND );
使用BufferedWriter 封装。
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(outputStream));
可以直接储存写入字符串
writer.write(data);
最后关闭 流对象
writer .close();

就完成了写入。
当然在java里 需要try 这些操作。否侧会报错的!


android 的Context提供了openFileinput()方法,所以我们可以借助这个方法来获取一个文件输入流
和以上的操作类似 只不过是获取了 输入流对象和openFileinput("文件名")只要这样就可以获取了。
FileInputStream inputStream = getActivity().openFileInput( "data" );
BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream));
String line = "" ;
while ( null != (line = reader.readLine())) {
  这里的line 就是读取的每行数据。
}

最后关闭 流对象
writer .close();
读取完毕

下面是C语言实现机与顺序表的学生成绩管理的代码,包括将数据保存到文件中和文件读取数据的两个功能。请根据需要适当修改。在本代码中,我们使用了结构体来存储学生信息和课程信息。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义课程信息结构体 struct CourseInfo { char courseName[50]; // 课程名称 float weight; // 平时成绩占比 }; // 定义学生信息结构体 struct StudentInfo { char name[50]; // 姓名 int id; // 学号 float dailyScore; // 平时成绩 float finalScore; // 期末成绩 float totalScore; // 总成绩 }; // 全局变量,存储课程信息 struct CourseInfo course; // 函数声明 void saveToFile(struct StudentInfo *students, int n, char *fileName); void loadFromFile(struct StudentInfo *students, int *n, char *fileName); int main() { // 定义学生信息数组 int n = 3; struct StudentInfo students[n]; // 初始化课程信息 strcpy(course.courseName, "数学"); course.weight = 0.3; // 初始化学生信息 strcpy(students[0].name, "张三"); students[0].id = 1001; students[0].dailyScore = 80.5; students[0].finalScore = 90.0; students[0].totalScore = students[0].dailyScore * course.weight + students[0].finalScore * (1 - course.weight); strcpy(students[1].name, "李四"); students[1].id = 1002; students[1].dailyScore = 75.0; students[1].finalScore = 85.5; students[1].totalScore = students[1].dailyScore * course.weight + students[1].finalScore * (1 - course.weight); strcpy(students[2].name, "王五"); students[2].id = 1003; students[2].dailyScore = 90.5; students[2].finalScore = 95.0; students[2].totalScore = students[2].dailyScore * course.weight + students[2].finalScore * (1 - course.weight); // 将学生信息保存到文件中 saveToFile(students, n, "students.txt"); // 从文件读取学生信息 loadFromFile(students, &n, "students.txt"); // 打印学生信息 printf("课程名称:%s\n", course.courseName); printf("平时成绩占比:%f\n", course.weight); printf("学生信息:\n"); for (int i = 0; i < n; i++) { printf("姓名:%s,学号:%d,平时成绩:%f,期末成绩:%f,总成绩:%f\n", students[i].name, students[i].id, students[i].dailyScore, students[i].finalScore, students[i].totalScore); } return 0; } // 将学生信息保存到文件中 void saveToFile(struct StudentInfo *students, int n, char *fileName) { // 打开文件 FILE *fp = fopen(fileName, "w"); if (fp == NULL) { printf("无法打开文件!\n"); exit(1); } // 写入课程信息 fprintf(fp, "%s\n", course.courseName); fprintf(fp, "%f\n", course.weight); // 写入学生信息 for (int i = 0; i < n; i++) { fprintf(fp, "%s %d %f %f %f\n", students[i].name, students[i].id, students[i].dailyScore, students[i].finalScore, students[i].totalScore); } // 关闭文件 fclose(fp); } // 从文件读取学生信息 void loadFromFile(struct StudentInfo *students, int *n, char *fileName) { // 打开文件 FILE *fp = fopen(fileName, "r"); if (fp == NULL) { printf("无法打开文件!\n"); exit(1); } // 读取课程信息 fscanf(fp, "%s", course.courseName); fscanf(fp, "%f", &course.weight); // 读取学生信息 int i = 0; while (fscanf(fp, "%s %d %f %f %f", students[i].name, &students[i].id, &students[i].dailyScore, &students[i].finalScore, &students[i].totalScore) != EOF) { i++; } *n = i; // 关闭文件 fclose(fp); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值