資源簡介
學生成績記錄簿設(shè)計
編制一個C語言成績記錄簿,每個學生信息包括:學號、姓名、C語言成績。具體功能:
(1)創(chuàng)建學生信息并以磁盤文件保存;
(2)讀取磁盤文件并顯示輸出所有學生的成績;
(3)按學號或姓名查詢成績;
(4)添加成績記錄;
(5)修改指定姓名或?qū)W號的學生的成績并可存盤;
(6)顯示輸出60分以下、60~79、80~89、90分以上各分數(shù)段的學生信息(可選項)。
(7)能將信息存儲到本地文本文件中

代碼片段和文件信息
import?java.io.*;
import?java.util.ArrayList;
class?FileIO?{
????public?static?void?read()?{
????????try?{?//?防止文件建立或讀取失敗,用catch捕捉錯誤并打印,也可以throw
????????????????/*?讀入TXT文件?*/
????????????String?pathname?=?“C語言成績管理系統(tǒng).txt“;?//?絕對路徑或相對路徑都可以,這里是絕對路徑,寫入文件時演示相對路徑
????????????File?filename?=?new?File(pathname);?//?要讀取以上路徑的input。txt文件
????????????InputStreamReader?reader?=?new?InputStreamReader(new?FileInputStream(filename)?“UTF-8“);?//?建立一個輸入流對象reader
????????????BufferedReader?br?=?new?BufferedReader(reader);?//?建立一個對象,它把文件內(nèi)容轉(zhuǎn)成計算機能讀懂的語言
????????????String?line?=?““;
????????????line?=?br.readLine();
????????????while?(line?!=?null)?{
????????????????line?=?br.readLine();?//?一次讀入一行數(shù)據(jù)
????????????????System.out.println(line);
????????????}
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
????public?static?void?write(ArrayList?students)?{
????????try?{
???????????????/*?寫入Txt文件?*/
????????????//?相對路徑,如果沒有則要建立一個新的output。txt文件
????????????File?writename?=?new?File(“save.txt“);
????????????writename.createNewFile();?//?創(chuàng)建新文件
????????????BufferedWriter?out?=?new?BufferedWriter(new?FileWriter(writename));
????????????for?(Student?stu?:?students)?{
????????????????out.write(stu.toString()?+?“\r\n“);?//?\r\n即為換行
????????????}
????????????out.flush();?//?把緩存區(qū)內(nèi)容壓入文件
????????????out.close();?//?最后記得關(guān)閉文件
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
????public?static?void?writeBySer(ArrayList?students)?{
????????try?{
????????????File?writename?=?new?File(“wxp.txt“);
????????????objectOutputStream?os?=?new?objectOutputStream(
????????????????????new?FileOutputStream(“wxp.txt“));
????????????os.writeobject(students);//?將List列表寫進文件
????????????System.out.println(“寫入成功“);
????????????os.close();
????????}?catch?(FileNotFoundException?e)?{
????????????e.printStackTrace();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}
????public?static?void?readBySer()?{
????????try?{
????????????objectInputStream?is?=?new?objectInputStream(new?FileInputStream(
????????????????????“wxp.txt“));
????????????ArrayList?students?=?(ArrayList)?is.readobject();//?從流中讀取List的數(shù)據(jù)
????????????for?(Student?student?:?students)?{
????????????????System.out.println(student);
????????????}
????????????is.close();
????????}?catch?(FileNotFoundException?e)?{
????????????e.printStackTrace();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}?catch?(ClassNotFoundException?e)?{
????????????e.printStackTrace();
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????571??2017-11-26?00:05??chengji\chengji.txt
?????文件????????329??2017-11-26?09:17??chengji\chengjip\.idea\artifacts\chengjip_jar.xm
?????文件????????734??2017-11-24?10:22??chengji\chengjip\.idea\compiler.xm
?????文件?????????97??2017-11-24?10:22??chengji\chengjip\.idea\desc
?????文件????????171??2017-11-24?10:22??chengji\chengjip\.idea\encodings.xm
?????文件????????489??2017-11-24?10:22??chengji\chengjip\.idea\misc.xm
?????文件????????263??2017-11-24?10:23??chengji\chengjip\.idea\modules.xm
?????文件????????173??2017-11-24?10:22??chengji\chengjip\.idea\vcs.xm
?????文件??????52485??2017-11-26?09:36??chengji\chengjip\.idea\workspace.xm
?????文件????????437??2017-11-24?10:22??chengji\chengjip\chengjip.iml
?????文件???????5930??2017-11-26?09:33??chengji\chengjip\out\artifacts\chengjip_jar\chengjip.jar
?????文件???????3531??2017-11-26?00:05??chengji\chengjip\out\production\chengjip\FileIO.class
?????文件???????5038??2017-11-26?00:05??chengji\chengjip\out\production\chengjip\Main.class
?????文件?????????43??2017-11-26?09:20??chengji\chengjip\out\production\chengjip\me
?????文件???????1441??2017-11-24?11:35??chengji\chengjip\out\production\chengjip\Student.class
?????文件????????168??2017-11-26?09:24??chengji\chengjip\save.txt
?????文件???????3031??2017-11-25?23:51??chengji\chengjip\src\FileIO.java
?????文件???????6493??2017-11-25?23:58??chengji\chengjip\src\Main.java
?????文件?????????43??2017-11-26?09:17??chengji\chengjip\src\me
?????文件???????1059??2017-11-24?11:28??chengji\chengjip\src\Student.java
?????文件????????199??2017-11-26?09:24??chengji\chengjip\wxp.txt
?????文件???????5930??2017-11-26?09:33??chengji\chengjip.jar
?????文件?????605475??2017-11-26?09:15??chengji\大作業(yè)完成模板---按此模板完成文檔并評分.docx
?????文件???????1349??2017-11-26?09:30??chengji\視頻.txt
?????目錄??????????0??2017-11-26?09:20??chengji\chengjip\out\production\chengjip\me
?????目錄??????????0??2017-11-26?09:33??chengji\chengjip\out\artifacts\chengjip_jar
?????目錄??????????0??2017-11-26?09:20??chengji\chengjip\out\production\chengjip
?????目錄??????????0??2017-11-26?09:17??chengji\chengjip\.idea\artifacts
?????目錄??????????0??2017-11-24?10:23??chengji\chengjip\.idea\inspectionProfiles
?????目錄??????????0??2017-11-26?09:33??chengji\chengjip\out\artifacts
............此處省略10個文件信息
評論
共有 條評論