package xml; import java.util.*; import java.io.*; public class TextFileTest { public static void main(String[] args){ Employee[] staff=new Employee[3]; staff[0]=new Employee("a",2500,1989,1,1); staff[1]=new Employee("b",2500,1989,1,1); staff[2]=new Employee("c",2500,1989,1,1); try{ PrintWriter out=new PrintWriter("employee.dat"); writeData(staff,out); out.close(); Scanner in=new Scanner(new FileReader("employee.dat")); Employee[] newStaff=readData(in); in.close(); for(Employee e:newStaff) System.out.println(e); }catch(IOException e){ System.out.println(e.toString()); } } public static void writeData(Employee[] employees,PrintWriter out){ out.println(employees.length); for(Employee e:employees) e.writeData(out); } public static Employee[] readData(Scanner in){ int n=in.nextInt(); in.nextLine(); Employee[] employees=new Employee[n]; for(int i=0;i<n;i++){ employees[i]=new Employee(); employees[i].readData(in); } return employees; } } class Employee{ private String name; private double salary; private Date hireDay; public Employee(){ } public Employee(String name,double salary,int year,int month,int day){ this.name=name; this.salary=salary; GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); this.hireDay=calendar.getTime(); } public String getName(){ return this.name; } public double getSalary(){ return this.salary; } public Date getHireDay(){ return this.hireDay; } public void raiseSalary(double byPersent){ double raise=this.salary*byPersent/100; this.salary+=raise; } public String toString(){ return getClass().getName()+"name="+name+",salary="+this.salary+",hireday="+this.hireDay+"]"; } public void writeData(PrintWriter out){ GregorianCalendar calendar=new GregorianCalendar(); calendar.setTime(this.hireDay); out.println(this.name+"|"+this.salary+"|"+calendar.get(Calendar.YEAR)+"|"+ calendar.get(Calendar.MONTH)+"|"+calendar.get(Calendar.DAY_OF_MONTH)); } public void readData(Scanner in){ String line=in.nextLine(); String[] tokens=line.split("\\|"); this.name=tokens[0]; this.salary=Double.parseDouble(tokens[1]); int y=Integer.parseInt(tokens[2]); int m=Integer.parseInt(tokens[3]); int d=Integer.parseInt(tokens[4]); GregorianCalendar calendar=new GregorianCalendar(y,m-1,d); this.hireDay=calendar.getTime(); } }