[Java] Đọc/Lưu file danh sách học sinh

Viết chương trình quản lý thông tin học sinh, tìm kiếm học sinh có năm sinh x và đọc lưu file danh sách học sinh java.

Xem thêm:

1. Bài toán

Bài 15:

Nguoi
– Hoten: String

– Namsinh: int

– Gioitinh: bool

+ getHoten():string

+getNamsinh(): int

+ getGioitinh(): bool

+Nguoi(in hoten: string, in namsinh: int, in gioitinh:bool)

 

Hocsinh
– tenlop: string
+ gioithieu(): string

+ getTenlop(): string

+ Hocsinh(in hoten:string, in namsinh: int, in gioitinh:bool, in tenlop:string)

  1. Tạo lớp Nguoi như trên.
  2. Tạo giao diện (interface) IHoatdong với phương thức string gioithieu().
  3. Tạo lớp Hocsinh thừa kể từ Nguoi và thi hành giao diện IHoatdong (cho biết học sinh học lớp nào)
  4. Viết chương trình:
  5. Nhập vào N học sinh
  6. Hiện N học sinh ra màn hình
  7. Nhập vào 1 năm sinh X, hiển thị ra màn hình tên từng năm đó và thông tin tự giới thiệu của từng học sinh. d. Lưu danh sách học sinh vào file
  8. Đọc danh sách học sinh có từ file và in lên màn hình

2. Code

2.1 Interface IHoatDong

package bai15;

public interface IHoatDong {
    public String gioithieu();
}

2.2 Lớp Nguoi

package bai15;

import java.io.Serializable;
import java.util.Scanner;

public class Nguoi implements Serializable{
    String hoTen;
    int namSinh;
    boolean gioiTinh;
    
    public Nguoi(){
        
    }
    
    public Nguoi(String hoTen, boolean gioiTinh, int namSinh){
        this.hoTen= hoTen;
        this.gioiTinh= gioiTinh;
        this.namSinh= namSinh;
    }

    public String getHoTen() {
        return hoTen;
    }

    public void setHoTen(String hoTen) {
        this.hoTen = hoTen;
    }

    public boolean isGioiTinh() {
        return gioiTinh;
    }

    public void setGioiTinh(boolean gioiTinh) {
        this.gioiTinh = gioiTinh;
    }

    public int getNamSinh() {
        return namSinh;
    }

    public void setNamSinh(int namSinh) {
        this.namSinh = namSinh;
    }
    
    public void nhap(){
        Scanner sc= new Scanner(System.in);
        byte i;
        while(true){
            try {
                System.out.println("Nhap ho va ten: ");
                hoTen= sc.nextLine();;
                System.out.println("Nhap nam sinh: ");
                namSinh= sc.nextInt();
                System.out.println("Gioi tinh(1:Nam - 0:Nu): ");
                i=sc.nextByte();
                if(namSinh<=0) throw new Exception(); break;
            } catch (Exception e) {
                System.out.print("Nhap sai gia tri, nhap lai!\n");
  sc.nextLine();
            }
        }
        sc.nextLine();
        if(i==1) gioiTinh=true; else gioiTinh=false;
    }
    
    public String hien(){
        String gt= null;
        if(gioiTinh) {
            gt="Nam";
        }else{
            gt="Nu";
        }
        String output = String.format("%-30s|%-8d|%-9s|",hoTen,namSinh,gt);
 return output;
    }
}

2.3 Lớp HocSinh

package bai15;

import java.io.Serializable;
import java.util.Scanner;

public class HocSinh extends Nguoi implements Serializable, IHoatDong{
    String lop;
    
    public HocSinh(){
        
    }
    
    public HocSinh(String hoTen, boolean gioiTinh, int namSinh, String lop){
        super(hoTen, gioiTinh, namSinh);
        this.lop= lop;
    }
    
    public void nhap(){
        Scanner sc= new Scanner(System.in);
        super.nhap();
        System.out.println("Nhap lop: ");
        lop= sc.nextLine();
    }

    @Override
    public String gioithieu() {
        String output = String.format(super.hien()+"%-7s",lop);
 return output;
    }
}

2.4 Lớp DanhSach đọc lưu file java

package bai15;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DanhSach {
    ArrayList<HocSinh> ds= new ArrayList<>();
    int n;
    Scanner sc = new Scanner(System.in);
    
    public void nhapDS(){
        System.out.println("Nhap so hoc sinh: ");
        n= sc.nextInt();
        for(int i=0;i<n;i++){
            System.out.println("\nHoc sinh thu "+(i+1));
            HocSinh hs= new HocSinh();
            hs.nhap();
            ds.add(hs);
        }
    }
    
    public void xuatDS() {
  System.out.println("\nDanh sach hoc sinh: ");
  System.out.printf("%-30s|%-8s|%-9s|%-7s\n","Ho ten","Nam sinh","Gioi tinh","Lop");
  for(HocSinh hs:ds) {
                    System.out.println(hs.gioithieu());
  }
 }
    
    public void timKiem(){
        int nam = 0, dem=0;
        while(true){
            try {
                System.out.println("Nhap nam sinh can tim: ");
                nam= sc.nextInt();
                if(nam<=0) throw new Exception();
                break;
            } catch (Exception e) {
                System.out.print("Nhap gia tri sai, nhap lai!\n");
  sc.nextLine();
            }
        }
        System.out.println("\nDanh sach học sinh: ");
 System.out.printf("%-30s|%-8s|%-9s|%-7s\n","Ho ten","Nam sinh","Gioi tinh","Lop");
            for(HocSinh hs:ds){
                if(hs.getNamSinh()==nam){
                    dem++;
                    System.out.println(hs.gioithieu());
                }
            }
            if(dem==0){
                System.out.println("Nam sinh khong ton tai");
            }
    }
    
    public void ghiFile() throws Exception{
        try {
            FileOutputStream fout = new FileOutputStream("bai15b.txt");
            ObjectOutputStream out = new ObjectOutputStream(fout);
            out.writeObject(ds);
            out.close();
            fout.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Ghi file khong thanh cong!");
        }
        System.out.println("Ghi file thanh cong");
        
    }
    
    public void docFile() throws Exception{
        try {
            FileInputStream fin = new FileInputStream("bai15b.txt");
            ObjectInputStream in = new ObjectInputStream(fin);
            ds = (ArrayList) in.readObject();
            in.close();
            fin.close();
            xuatDS();
        }catch(FileNotFoundException e){
            System.out.println("Khong tim thay file");
        } 
        catch (Exception e) {
            e.printStackTrace();
            System.out.println("Ghi file khong thanh cong");
        }
    }
}

2.5 Lớp main

package bai15;

import java.util.Scanner;

public class Bai15 {
    
    static void menu(){
        System.out.println("1. Nhap danh sach hoc sinh");
        System.out.println("2. Xuat danh sach hoc sinh");
        System.out.println("3. Loc danh sach theo nam sinh");
        System.out.println("4. Ghi file");
        System.out.println("5. Doc File");
        System.out.println("0. Thoat");
    }
    public static void main(String[] args) throws Exception {
        DanhSach d = new DanhSach();
        int chon;
        Scanner sc = new Scanner(System.in);
        
        do {            
            menu();
            System.out.println("Lua chon: ");
            chon = sc.nextInt();
            switch(chon){
                case 1: d.nhapDS();break;
                case 2: d.xuatDS();break;
                case 3: d.timKiem();break;
                case 4: d.ghiFile();break;
                case 5: d.docFile();break;
                case 0: System.exit(0); break;
                default: break;
            }
        } while (chon!=0);
    }
    
}

3. Kết quả

Trên đây là chương trình đọc lưu file danh sách học sinh bằng ngôn ngữ java. Cảm ơn các bạn đã tham khảo trên ttnguyen.net.

Nguyễn Tiến Trường

Mình viết về những điều nhỏ nhặt trong cuộc sống, Viết về câu chuyện những ngày không có em