trong ảnh là lỗi ạ; dưới là code , e không hiểu sao đến lần gọi hàm nhập thứ 2 đến chỗ đọc n thì nó văng ra lỗi mn jup e vs
package dssv;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class Main {
public static void nhap() throws IOException
{
Scanner sc= new Scanner(System.in);
File f= new File("danhsach1.txt");
FileOutputStream fo =null;
ObjectOutputStream oo=null;
if(!f.exists())
{
f.createNewFile();
fo= new FileOutputStream(f);
oo =new ObjectOutputStream(fo);
}else
{
fo= new FileOutputStream(f,true);
oo =new ObjectOutputStream(fo) {
protected void writeStreamHeader() throws IOException
{
reset();
}
};
}
int n=0;
SinhVien sv= new SinhVien();
System.out.println("nhap so sv muon nhap:");
n=Integer.parseInt(sc.nextLine());
for(int i =0 ; i<n;i++)
{
String name;
int id;
System.out.println("ten:");
name=sc.nextLine();
sv.setName(name);
System.out.println("id:");
id =sc.nextInt();
sc.nextLine();
sv.setId(id);
System.out.println("---------------");
oo.writeObject(sv);
}
sc.close();
fo.close();
}
public static void doc()
{
try {
FileInputStream fi =new FileInputStream("danhsach1.txt");
ObjectInputStream oi =new ObjectInputStream(fi);
SinhVien sv= null;
try {
do {
sv= (SinhVien)oi.readObject();
System.out.println("ten:"+sv.getName());
System.out.println("id:"+sv.getId());
}while(sv!=null);
}catch(Exception e)
{
System.out.println("end!");
System.out.println(e);
}finally {
fi.close();
}
}catch(Exception e)
{
System.out.println("file chua ton tai!");
System.out.println(e);
}
}
public static void main(String[] args) throws IOException {
int x= 0;
nhap();
doc();
nhap();
doc();
}
}