By GokiSoft.com| 15:19 28/10/2022|
Java Advanced

[Source Code] Tìm hiểu Generic & File trong Java - Khoá học Java nâng cao - C2109I

- Generic:
- File: Doc/ghi noi dung du lieu
	Text
	Binary

#Main.java

package java2.lesson02;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Main {

    public static void main(String[] args) {
        Map<String, Student> map = new HashMap<>();

        Scanner scan = new Scanner(System.in);
        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    System.out.println("Nhap so sinh vien can them: ");
                    int N = Integer.parseInt(scan.nextLine());
                    
                    for (int i = 0; i < N; i++) {
                        Student std = new Student();
                        std.input();
                        
                        map.put(std.getRollno(), std);
                    }
                    break;
                case 2:
                    for (Map.Entry<String, Student> entry : map.entrySet()) {
                        Student std = entry.getValue();
                        std.display();
                    }
                    break;
                case 3:
                    System.out.println("Nhap MSV can tim: ");
                    String rollno = scan.nextLine();
                    
                    if(map.containsKey(rollno)) {
                        map.get(rollno).display();
                    } else {
                        System.out.println("Khong tim thay SV nao.");
                    }
                    break;
                case 4:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while (choose != 4);
    }

    static void showMenu() {
        System.out.println("1. Nhap sinh vien");
        System.out.println("2. Hien thi");
        System.out.println("3. Tim kiem thong tin sinh vien");
        System.out.println("4. Thoat");
        System.out.println("Chon: ");
    }
}

#Student.java

package java2.lesson02;

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

/**
 *
 * @author diepvan
 */
public class Student implements Serializable{
    String rollno, name, gender, email, address;
    int age;

    public Student() {
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap MSV: ");
        rollno = scan.nextLine();
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap gioi tinh: ");
        gender = scan.nextLine();
        System.out.println("Nhap email: ");
        email = scan.nextLine();
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
        
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(scan.nextLine());
    }

    @Override
    public String toString() {
        return "rollno=" + rollno + ", name=" + name + ", gender=" + gender + ", email=" + email + ", address=" + address + ", age=" + age;
    }

    public void display() {
        System.out.println(this);
    }
    
    public String getRollno() {
        return rollno;
    }

    public void setRollno(String rollno) {
        this.rollno = rollno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    
}

#Test.java

package java2.lesson02;

/**
 *
 * @author diepvan
 */
public class Test {
    public static void main(String[] args) {
        Student std = new Student();
        std.input();
    }
}

#Test2.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author diepvan
 */
public class Test2 {
    public static void main(String[] args) {
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(12);
        list1.add(22);
        list1.add(32);
        
        System.out.println(list1.get(0));
        
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("23");
        list2.add("2343");
        list2.add("235fdg");
        
        System.out.println(list2.get(1));
        
        HashMap<String, Integer> map = new HashMap<>();
    }
}

#Test3.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson02;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Test3 {
    public static void main(String[] args) {
        //test01();
//        test02();
//        test03();

//        test04();
        test05();
    }
    
    static void test05() {
        List<Student> dataList;
        
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("students.dat");
            ois = new ObjectInputStream(fis);
            
            dataList = (List<Student>) ois.readObject();
            
            for (Student student : dataList) {
                student.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    
    static void test04() {
        List<Student> dataList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Student std = new Student();
            std.input();
            
            dataList.add(std);
        }
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("students.dat");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(dataList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void test03() {
        String filePath = "vidu.txt";
        
        FileInputStream fis = null;
        
        try {
            fis = new FileInputStream(filePath);
            
            byte[] data = fis.readAllBytes();
            
            String content = new String(data);
            System.out.println(content);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void test02() {
        String filePath = "vidu.txt";
        
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(filePath, true);
//            fos = new FileOutputStream(filePath);
            
            String content = "\nSinh vien Aptech 54 Le Thanh Nghi";
            byte[] b = content.getBytes("utf8");
            fos.write(b);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void test01() {
        File f = new File("vidu.txt");
        
        if(f.exists()) {
            System.out.println("File ton tai");
        } else {
            System.out.println("File khong ton tai");
        }
    }
}
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó