Mình không hiểu bị lỗi gì đấy mn à - nó bị xuất ra dữ liệu gì á
Đây là code ghi file nè
public class FileManager {
public static final String STUDENT_FILE_NAME = ("student.txt");
/**
* save list student to file
*
* @param studentList: list student to save
*/
public void write(List<Staff> studentList)throws FileNotFoundException {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
FileInputStream fw = new FileInputStream("student.txt");
FileOutputStream rr = new FileOutputStream("student.txt");
Writer out = new OutputStreamWriter(rr, "UTF8");
fos = new FileOutputStream(new File(STUDENT_FILE_NAME));
ObjectOutputStream ss = new ObjectOutputStream(rr);
ss = new ObjectOutputStream(fos);
ss.writeObject(studentList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeStream(fos);
closeStream(oos);
}
}
/**
* read list student from file
*
* @return list student
*/
public List<Staff> read() {
List<Staff> studentList = new ArrayList<>();
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(new File(STUDENT_FILE_NAME));
ois = new ObjectInputStream(fis);
studentList = (List<Staff>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
closeStream(fis);
closeStream(ois);
}
return studentList;
}
/**
* close input stream
*
* @param is: input stream
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* close output stream
*
* @param os: output stream
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Staff ReadFile(){
Staff c=new Staff();
try {
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj= ois.readObject();
c = (Staff) obj;
System.out.println(c.toString());
ois.close();
System.out.println("Press Any Key To Continue...");
new java.util.Scanner(System.in).nextLine();
} catch (Exception ex) {
System.out.println(ex);
}
return c;
}
}
Còn đây là code nhân dữ liệu từ ArrayList khi nhập vào nè
public class ManageStaff implements Serializable{
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
ArrayList<Staff> listStaff= new ArrayList<>();
ArrayList<Marketing> Mkt = new ArrayList<>();
ArrayList<Manager> MaNa = new ArrayList<>();
Staff staff=new Staff();
Scanner input = new Scanner(System.in);
ClearDisplay Cls=new ClearDisplay();
private FileManager fileManager;
List<Staff> ss;
// lưu thông tin nhân viên
public ManageStaff(){
fileManager = new FileManager();
ss = fileManager.read();
}
/// NHAP NHAN VIEN ( nhan viên )
public void InputStaff(){
boolean check= false;
while(!check){
System.out.print(" ");
try{
Cls.cls();
System.out.print("Nhập số lượng nhân viên cần nhập : ");
int Amount=input.nextInt();
FileWriter fw = new FileWriter("student.txt");
for (int i = 0; i < Amount; i++) {
Staff staff = new Staff("", "",0);
staff.inputStaff();
listStaff.add(staff);
fileManager.write(listStaff);
}
}
catch(Exception e){
System.out.println("Giá trị phải là số, hãy nhập lại...");
input.nextLine();
break;
}
break;
}
}
}