Đây là code mình viết như không đọc đk dữ liệu nhập kể từ lần thứ 2 trở đi
Class Function
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ObjectIO;
import ListObjectIO.ObRead;
import ListObjectIO.ObWirte;
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.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Tien Nguyen
*/
public class Function {
public Function() {
ArrayList<Studental = new ArrayList();
boolean a = false;
Student a1 = new Student(555, "nnnnnnnnnnnnnnnn", "mmmmmmmmmmmmmmmmm", 5, 10);
Student a2 = new Student(555, "dddddddddddddd", "cccccccccccccccccccccccccccc", 5, 10);
Student a3 = new Student(555, "wwwwwwwwwwwwwwwwwwwwwwwwwww", "aaaaaaaaaaaâ", 5, 10);
al.add(a1);
al.add(a2);
al.add(a3);
ObWrite(al, false);
}
public void Input() {
ArrayList<Studental = new ArrayList();
Scanner s = new Scanner(System.in);
System.out.println("=========== Them moi sinh vien =============");
System.out.print("Nhap ID: ");
int id = s.nextInt();
s = new Scanner(System.in);
System.out.println("Nhap ten: ");
String name = s.nextLine();
System.out.println("Nhap Lop: ");
String classs = s.nextLine();
System.out.println("Nhap diem 1: ");
int mark1 = s.nextInt();
System.out.println("Nhap diem 1: ");
int mark2 = s.nextInt();
Student st = new Student(id, name, classs, mark1, mark2);
al.add(st);
ObWrite(al, true);
//a = true;
}
public void ObWrite(ArrayList st, boolean append) {
String path = "src\\ObjectIO\\Student.dat";
File file = new File(path);
OutputStream outputStream;
try {
ObjectOutputStream ois;
if (append) {
outputStream = new FileOutputStream(file, append);
ois = new ObjectOutputStream(outputStream) {
@Override
protected void writeStreamHeader() throws IOException {
}
};
} else {
outputStream = new FileOutputStream(file);
ois = new ObjectOutputStream(outputStream);
}
ois.writeObject(st);
ois.flush();
ois.close();
outputStream.flush();
outputStream.close();
} catch (IOException ex) {
Logger.getLogger(Function.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ArrayList<StudentreadAppend() {
String path = "src\\ObjectIO\\Student.dat";
File f = new File(path);
ArrayList<Studentres = new ArrayList<>();
ArrayList<Studentres2 = new ArrayList<>();
ObjectInputStream obin = null;
try {
FileInputStream in = new FileInputStream(f);
obin = new ObjectInputStream(in);
res = (ArrayList<Student>) obin.readObject();
res2 = (ArrayList<Student>) obin.readObject();
System.out.println("ab <-----------------------------------------------");
for (int i = 0; i < res2.size(); i++) {
System.out.println(i + " " + res2.get(i).getId() + "\t" + res2.get(i).getName() + "\t" + res2.get(i).getClasss() + "\t" + res2.get(i).getMark1() + "\t" + res2.get(i).getMark2());
}
} catch (Exception ex) {
} finally {
if (obin != null) {
try {
obin.close();
System.out.println("---------------- Error -------------");
} catch (IOException ex) {
Logger.getLogger(ObRead.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return res;
}
public void ShowStudent(ArrayList<Studentst) {
System.out.println("NO StudentID\tStudentName\tClass \tMark\tMark");
for (int i = 0; i < st.size(); i++) {
System.out.println(i + " " + st.get(i).getId() + "\t" + st.get(i).getName() + "\t" + st.get(i).getClasss() + "\t" + st.get(i).getMark1() + "\t" + st.get(i).getMark2());
}
}
}
Class Student
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ObjectIO;
import java.io.Serializable;
/**
*
* @author Tien Nguyen
*/
public class Student implements Serializable{
private int id;
private String name;
private String classs;
private int mark1;
private int mark2;
public Student(int id, String name, String classs, int mark1, int mark2) {
this.id = id;
this.name = name;
this.classs = classs;
this.mark1 = mark1;
this.mark2 = mark2;
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasss() {
return classs;
}
public void setClasss(String classs) {
this.classs = classs;
}
public int getMark1() {
return mark1;
}
public void setMark1(int mark1) {
this.mark1 = mark1;
}
public int getMark2() {
return mark2;
}
public void setMark2(int mark2) {
this.mark2 = mark2;
}
}
Class main
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ObjectIO;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Tien Nguyen
*/
public class Run {
public static void main(String[] args) {
Function f = new Function();
ArrayList<Studentsl = null;
while (true) {
System.out.println("1. Them moi sinh vien");
System.out.println("2. Xem danh sach sinh vien");
Scanner s = new Scanner(System.in);
System.out.print("#chon: ");
int choice = s.nextInt();
switch (choice) {
case 1:
f.Input();
break;
case 2:
sl = f.readAppend();
f.ShowStudent(sl);
break;
}
}
}
}