Код | 1. import java.io.*; 2. import java.util.*; 3. 4. public class DataFileTest 5. { 6. public static void main(String[] args) 7. { 8. Employee[] staff = new Employee[3]; 9. 10. staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); 11. staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); 12. staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); 13. 14. try 15. { 16. // save all employee records to the file employee.dat 17. PrintWriter out = new PrintWriter(new FileWriter("employee.dat")); 18. writeData(staff, out); 19. out.close(); 20. 21. // retrieve all records into a new array 22. BufferedReader in = new BufferedReader(new FileReader("employee.dat")); 23. Employee[] newStaff = readData(in); 24. in.close(); 25. 26. // print the newly read employee records 27. for (Employee e : newStaff) 28. System.out.println(e); 29. } 30. catch(IOException exception) 31. { 32. exception.printStackTrace(); 33. } 34. } 35. 36. /** 37. Writes all employees in an array to a print writer 38. @param employees an array of employees 39. @param out a print writer 40. */ 41. static void writeData(Employee[] employees, PrintWriter out) 42. throws IOException 43. { 44. // write number of employees 45. out.println(employees.length); 46. 47. for (Employee e : employees) 48. e.writeData(out); 49. } 50. 51. /** 52. Reads an array of employees from a buffered reader 53. @param in the buffered reader 54. @return the array of employees 55. */ 56. static Employee[] readData(BufferedReader in) 57. throws IOException 58. { 59. // retrieve the array size 60. int n = Integer.parseInt(in.readLine()); 61. 62. Employee[] employees = new Employee[n]; 63. for (int i = 0; i < n; i++) 64. { 65. employees[i] = new Employee(); 66. employees[i].readData(in); 67. } 68. return employees; 69. } 70. } 71. 72. class Employee 73. { 74. public Employee() {} 75. 76. public Employee(String n, double s, int year, int month, int day) 77. { 78. name = n; 79. salary = s; 80. GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); 81. hireDay = calendar.getTime(); 82. } 83. 84. public String getName() 85. { 86. return name; 87. } 88. 89. public double getSalary() 90. { 91. return salary; 92. } 93. 94. public Date getHireDay() 95. { 96. return hireDay; 97. } 98. 99. public void raiseSalary(double byPercent) 100. { 101. double raise = salary * byPercent / 100; 102. salary += raise; 103. } 104. 105. public String toString() 106. { 107. return getClass().getName() 108. + "[name=" + name 109. + ",salary=" + salary 110. + ",hireDay=" + hireDay 111. + "]"; 112. } 113. 114. /** 115. Writes employee data to a print writer 116. @param out the print writer 117. */ 118. public void writeData(PrintWriter out) throws IOException 119. { 120. GregorianCalendar calendar = new GregorianCalendar(); 121. calendar.setTime(hireDay); 122. out.println(name + "|" 123. + salary + "|" 124. + calendar.get(Calendar.YEAR) + "|" 125. + (calendar.get(Calendar.MONTH) + 1) + "|" 126. + calendar.get(Calendar.DAY_OF_MONTH)); 127. } 128. 129. /** 130. Reads employee data from a buffered reader 131. @param in the buffered reader 132. */ 133. public void readData(BufferedReader in) throws IOException 134. { 135. String s = in.readLine(); 136. StringTokenizer t = new StringTokenizer(s, "|"); 137. name = t.nextToken(); 138. salary = Double.parseDouble(t.nextToken()); 139. int y = Integer.parseInt(t.nextToken()); 140. int m = Integer.parseInt(t.nextToken()); 141. int d = Integer.parseInt(t.nextToken()); 142. GregorianCalendar calendar = new GregorianCalendar(y, m - 1, d); 143. hireDay = calendar.getTime(); 144. } 145. 146. private String name; 147. private double salary; 148. private Date hireDay; 149. }
|
|