import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class DemoObjectStream {

	static String pathName = "C:/Users/ta/Desktop/javaProgram/abc";
public static void writeFile(String path) {
		ObjectOutputStream out = null;
try {
			out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
			NhanVien nv00 = new NhanVien("A", 18, 4.0);
			NhanVien nv01 = new NhanVien("B", 19, 6.0);
			NhanVien nv02 = new NhanVien("C", 20, 5.0);
			NhanVien nv03 = new NhanVien("D", 21, 7.0);
			out.writeObject(nv00);
			out.writeObject(nv01);
			out.writeObject(nv02);
			out.writeObject(nv03);
			System.out.println("Kết thúc chương trình !");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void readFile(String path) {
		ObjectInputStream input = null;
		try {
			input = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));
			NhanVien nv = (NhanVien) input.readObject();
			System.out.println("Tên nhân viên 1 :" + nv.getName());

			nv = (NhanVien) input.readObject();
			System.out.println("Tên nhân viên 2 :" + nv.getName());

			nv = (NhanVien) input.readObject();
			System.out.println("Tên nhân viên 3 :" + nv.getName());

			nv = (NhanVien) input.readObject();
			System.out.println("Tên nhân viên 4 :" + nv.getName());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {

		writeFile(pathName);
		readFile(pathName);

	}

}
