Java實驗5 IO流 第四題
阿新 • • 發佈:2018-12-01
設計學生類Student,屬性:學號(整型);姓名(字串),選修課程(名稱)及課程成績(整型)。編寫一個控制檯程式,能夠實現Student資訊的儲存、讀取。具體要求:(1)提供Student資訊的儲存功能:通過控制檯輸入若干個學生的學號、姓名以及每個學生所修課程的課程名和成績,將其資訊儲存到data.dat中;(2)資料讀取顯示:能夠從data.dat檔案中讀取學生及其課程成績並顯示於控制檯。
import java.io.*; import java.util.*; public class Student { private int no; private String name; private String subject; private int score; public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public String toString() { return "Student [no=" + no + ", name=" + name + ", subject=" + subject + ", score=" + score + "]"; } public static void saveStudent(List<Student> students) throws IOException { try { BufferedWriter out=new BufferedWriter(new FileWriter("data.dat",true)); StringBuffer s=new StringBuffer(); for(int i=0;i<students.size();i++) { Student stu=students.get(i); s.append(stu.getNo()+"#"+stu.getName()+"#"+stu.getSubject()+"#"+stu.getScore()); out.write(s.toString()); out.write("\n"); } out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static List<Student> readStudent(String filename) throws IOException{ File file=new File(filename); List<Student> data=new ArrayList<Student>(); if(file.exists()) { BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(file))); String s=null; String[] str=null; while(true) { s=in.readLine(); if(s!=null) { Student stu=new Student(); str=s.split("#"); stu.setNo(Integer.parseInt(str[0])); stu.setName(str[1]); stu.setSubject(str[2]); stu.setScore(Integer.parseInt(str[3])); data.add(stu); } else break; } in.close(); } return data; } public static void show() { System.out.println("退出請輸入#0"); System.out.println("錄入學生資訊請輸入#1"); System.out.println("檢視學生資訊請輸入#2"); System.out.println("儲存學生資訊請輸入#3"); } }
import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scan=new Scanner(System.in); List<Student>data=new ArrayList<Student>(); Student.show(); Student stu=new Student(); while(true) { String s=scan.next(); if(s.equals("#0")) { System.out.println("已退出輸入"); break; } else if(s.equals("#1")) { String temp=null; System.out.println("請依次“輸入學號#姓名#選修課程#課程成績”"); while(true) { System.out.print("學號:"); int No=scan.nextInt(); stu.setNo(No); System.out.print("姓名:"); String Name=scan.next(); stu.setName(Name); System.out.print("選修課程:"); String Sub=scan.next(); stu.setSubject(Sub); System.out.print("課程成績:"); int Score=scan.nextInt(); stu.setScore(Score); data.add(stu); System.out.println("輸入exit結束錄入,輸入其他繼續錄入"); temp=scan.next(); if(temp.equals("exit")) { System.out.println("已退出錄入"); break; } } } else if(s.equals("#2")) { List<Student> stus=Student.readStudent("data.dat"); if(stus==null||stus.size()==0) { System.out.println("無學生資訊!"); }else { for(int i=0;i<stus.size();i++) { System.out.println(stus.get(i).toString()); } } } else if(s.equals("#3")) { if(data.size()>0) { Student.saveStudent(data); data.clear(); System.out.println("資訊已儲存"); }else { System.out.println("無需要儲存的學生資訊!"); } } else { System.out.println("指令錯誤!"); Student.show(); } } scan.close(); } }