Dlaczego mi tutaj wyskakuje IOException przy zapisie i przy odczytywaniu z pliku?
package Data;
import java.io.IOException;
public class App {
public static void main(String[] args){
FileManager fileManager = new FileManager();
Staff staff = new Staff();
staff.add(new Employee("Jan","Kowalski", 2010, 'M',3));
staff.add(new Employee("Agnieszka","Cuber",2900,'K',3));
staff.add(new Employee("Adam", "Nowak", 1100, 'M', 4));
staff.add(new Employee("Sylwia", "Zych", 2100, 'K', 3));
staff.add(new Employee("Beata", "Dudek", 1900, 'K', 4));
fileManager.WriteToFile(staff);
try {
fileManager.readFromFile();
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
Employee[] e = staff.getEmployee();
for(int i = 0;i<staff.getEmployeesNumber();i++){
System.out.println(e[i]);
}
}
}
package Data;
import java.io.Serializable;
public class Employee implements Serializable{
private String firstName;
private String lastName;
private int salary;
private char gender;
private int section;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getSalary() {
return salary;
}
public int getSection() {
return section;
}
public char getGender() {
return gender;
}
public Employee(){};
public Employee(String firstName, String lastName, int salary, char gender, int section){
this.firstName=firstName;
this.lastName=lastName;
this.salary=salary;
this.gender=gender;
this.section=section;
}
public String toString(){
return firstName + " " + lastName + " Wynagrodzenie: " + salary + " Płeć: " + gender + " Dział: " + section;
}
}
package Data;
import java.io.*;
public class FileManager {
private final String FILE_NAME = "staff.txt";
public void WriteToFile(Staff p){
try(
FileOutputStream ios = new FileOutputStream(FILE_NAME);
ObjectOutputStream out = new ObjectOutputStream(ios);
){
out.writeObject(p);
} catch(IOException e){
System.out.println("Błąd podczas zapisu danych");
}
}
public Employee readFromFile() throws FileNotFoundException, ClassNotFoundException, IOException {
Employee p1 = null;
try (
FileInputStream fis = new FileInputStream(FILE_NAME);
ObjectInputStream in = new ObjectInputStream(fis);
) {
p1 = (Employee) in.readObject();
} catch (FileNotFoundException e) {
System.out.println("Nie znaleziono pliku");
} catch (IOException e) {
e.printStackTrace();
// System.out.println("Błąd podczas wczytywania danych");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return p1;
}
}
package Data;
public class Staff {
private static final int MAX_EMPLOYEES = 100;
private Employee[] employee;
private int employeesNumber;
public Employee[] getEmployee() {
return employee;
}
public Staff() {
employee = new Employee[MAX_EMPLOYEES];
employeesNumber=0;
}
public void add(Employee e) {
if (employeesNumber<MAX_EMPLOYEES){
employee[employeesNumber]=e;
employeesNumber++;
}
}
}