Friday, October 14, 2016

BÃ i 28 ArrayList trong Java 2

BÃ i 28 ArrayList trong Java 2


Bài 28: ArrayList trong Java (2)

ArrayListWith5Points
?
? bài vi?t này, mình s? chia s? v? cách l?u l?i d? li?u khi trong ch??ng trình s? d?ng ArrayList.
Th?c ra, k?t h?p v?i bài tr??c, bài ??c ghi Object, s? có nhi?u b?n làm ???c ?i?u này ngay. Nh?ng mình s? vi?t ?? cho m?t s? b?n c?m th?y khó kh?n tham kh?o luôn. Mình s? chú thích trong code!
V? cách l?u lên file ? ch??ng trình có dùng ArrayList, mình dùng 2 cách nh? sau

Cách 1: L?u tr? các ??i t??ng trong m?t ??i t??ng ArrayList xu?ng file!
Ch??ng trình d??i ?ây s? l?u toàn b? các ??i t??ng h?c sinh xu?ng file, ?? thao tác, ta n?p các ??i t??ng này và 1 ??i t??ng ArrayList
PHP:
package javaandroidvn;
 
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.Serializable;import java.util.ArrayList;import java.util.Scanner;import java.util.logging.Level;import java.util.logging.Logger;

class 
HocSinh implements Serializable {

    private 
String ten;
    private 
int tuoi;

    public 
String getTen() {
        return 
ten;
    }

    public 
void setTen(String ten) {
        
this.ten ten;
    }

    public 
int getTuoi() {
        return 
tuoi;
    }

    public 
void setTuoi(int tuoi) {
        
this.tuoi tuoi;
    }
}

public class 
JavaAndroidVn {

    public static 
void main(String[] argsthrows IOException {
        
ArrayList<HocSinhlistHS = new ArrayList(); // Khai báo 1 ??i t??ng ArrayList, các ph?n t? t?o ra t? l?p HocSinh

        
Scanner input = new Scanner(System.in);
        
System.out.println("Nh?p s? h?c sinh: ");
        
int n input.nextInt();

        
//L?y d? li?u và ghi vào file
        
try {
            
FileOutputStream f = new FileOutputStream("E:hocsinh.dat");
            
ObjectOutputStream oOT = new ObjectOutputStream(f); // S? d?ng ?? ghi file theo t?ng Object
            
for (int i 0ni++) {
                
HocSinh x = new HocSinh(); //T?o ??i t??ng x ?? l?u t?m th?i d? li?u
                
System.out.println("H?c sinh th? " ": ");
                
input.nextLine(); //Dòng này ?? tránh b? tr??t dòng!

                
System.out.print("Tên: ");
                
String tenX input.nextLine();

                
System.out.print("Tu?i: ");
                
int tuoiX input.nextInt();

                
x.setTen(tenX);
                
x.setTuoi(tuoiX);

                
oOT.writeObject(x); // Ghi  Object là ??i t??ng x xu?ng file

            
}
        } catch (
FileNotFoundException ex) {
            
Logger.getLogger(JavaAndroidVn.class.getName()).log(Level.SEVEREnullex);
        }
   
        
//??c d? li?u t? file, l?y các object ra r?i gán vào ListHS
        
try {
            
FileInputStream f2 = new FileInputStream("E:hocsinh.dat");
            
ObjectInputStream oIT = new ObjectInputStream(f2); // S? d?ng ?? ??c file theo t?ng Object
            
HocSinh x = new HocSinh(); //T?o ??i t??ng x ?? l?u t?m th?i d? li?u
            
for (int i 0ni++) {
                
= (HocSinhoIT.readObject(); //??c Object ??u tiên ép ki?u v? ki?u SinhVien sau ?ó gán b?ng ??i t??ng a1
                
listHS.add(x);
            }
            
oIT.close();
            
f2.close();
        } catch (
IOException io) {
            
System.out.println("Có l?i x?y ra!");
        } catch (
ClassNotFoundException ex) {
            
System.out.println("Không tìm th?y class");
        }

        
//In thông tin ra ngoài!
        
System.out.println("Thông tin danh sách v?a nh?p vào là: ");
        for (
int i 0listHS.size(); i++) {
            
System.out.print("H?c sinh th? " i);
            
System.out.print(" - Tên " listHS.get(i).getTen() + " Tu?i: " listHS.get(i).getTuoi() + " ");
        }


    }
}
Cách 2: L?u ??i t??ng ArrayList xu?ng File
Vì b?n thân listHS là 1 ??i t??ng, nên ng?n g?n ta ch? c?n l?u tr? ??i t??ng listHS là ???c!
PHP:
package javaandroidvn;
 
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.Serializable;import java.util.ArrayList;import java.util.Scanner;import java.util.logging.Level;import java.util.logging.Logger;

class 
HocSinh implements Serializable {

    private 
String ten;
    private 
int tuoi;

    public 
String getTen() {
        return 
ten;
    }

    public 
void setTen(String ten) {
        
this.ten Available link for download