package studentmanage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileOperate {
String fileName="student.dat";
List
File file;
//创建文件
public FileOperate(){
file=new File(this.fileName);
if(file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//增加学生
public void insert(){
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
try {
System.out.println("请输入姓名:");
String inputName=br.readLine();
System.out.println("请输入学号:");
String inputNum=br.readLine();
System.out.println("请输入性别:");
String inputGender=br.readLine();
Student stu=new Student(inputName,inputNum,inputGender);
stuList=getAll();
stuList.add(stu);
FileOutputStream fos=new FileOutputStream(this.fileName);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(stuList);
oos.close();
fos.close();
System.out.println("增加学生成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private List
try {
InputStream fis=new FileInputStream(this.fileName);
ObjectInputStream ois=new ObjectInputStream(fis);
this.stuList=(List
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}