首页 > 代码库 > 集合与IO流结合的练习

集合与IO流结合的练习

Edit

 

集合与IO流结合的练习

练习:
有五个学生,每个学生有3门课程:数学,语文,英语
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:zhangsan, 30, 40, 40计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件”stud.txt”中.

1.描述学生对象;
2.定义一个可操作学生对象的工具类;

思路:
1.通过获取键盘录入的一行数据,并将录入的信息封装成学生对象;
2.因为学生对象很多,且要对学生的总分排序,所以用TreeSet集合;
3.将集合中的信息写入到文件中;


1.import java.io.*;
2.import java.util.*;
3.class StudentInfoTest
4.
{
5. public static void main(String[] args) throws IOException
6.
{
7. Comparator<Student> comp = Collections.reverseOrder();//因为之前的排序是从小到大排,现在从大到小排,所以定义比较器
8. Set<Student> stus = StudentInfoTool.getStudent(comp);
9. StudentInfoTool.writeToFile(stus);
10. }
11.}
12.class Student implements Comparable<Student>
13.
{
14. private String name;
15. private int ma,cn,en;
16. private int sum;
17.
18. Student(String name,int ma,int cn,int en)
19. {
20. this.name = name;
21. this.ma = ma;
22. this.cn = cn;
23. this.en = en;
24. sum = ma + cn + en;
25. }
26. public String getName()
27.
{
28. return name;
29. }
30. public int getSum()
31.
{
32. return sum;
33. }
34. public String toString()
35.
{
36. return "student["+name+", "+ma+", "+cn+", "+en+",]";
37. }
38. public int compareTo(Student stu)
39.
{
40. int num = new Integer(this.sum).compareTo(new Integer(stu.sum));
41. if (num==0)
42. {
43. return this.name.compareTo(stu.name);
44. }
45. return num;
46. }
47. public int hashCode()
48.
{
49. return name.hashCode()+sum*39;
50. }
51. public boolean equals(Object obj)
52.
{
53. if (!(obj instanceof Student))
54. {
55. throw new ClassCastException("传入类型不正确");
56. }
57. Student stu = (Student)obj;
58. return this.name.equals(stu.name) && this.sum==stu.sum;
59. }
60.}
61.class StudentInfoTool
62.
{
63. public static Set<Student> getStudent() throws IOException
64.
{
65. getStudent(null);
66. return null;
67. }
68. public static Set<Student> getStudent(Comparator<Student> comp) throws IOException
69.
{
70. BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
71. String line = null;
72. Set<Student> stus = null;
73. if (comp==null)
74. {
75. stus = new TreeSet<Student>();
76. }
77. else
78. {
79. stus = new TreeSet<Student>(comp);
80. }
81. while ((line=bufr.readLine()) !=null)
82. {
83. if ("over".equals(line))
84. {
85. break;
86. }
87. String[] arr = line.split(",");
88. Student stu = new Student(arr[0],Integer.parseInt(arr[1]),
89. Integer.parseInt(arr[2]),
90. Integer.parseInt(arr[3]));
91. stus.add(stu);
92. }
93. bufr.close();
94. return stus;
95. }
96. public static void writeToFile(Set<Student> stus) throws IOException
97.
{
98. BufferedWriter bufw = new BufferedWriter(new FileWriter("e:\\stu.txt"));
99. for (Student stu : stus)
100. {
101. bufw.write(stu.toString());
102. bufw.write(stu.getSum()+"");
103. bufw.newLine();
104. bufw.flush();
105. }
106. bufw.close();
107. }
108.
109.}
110.
111.
@%2802.%u8D44%u6599%29%5B%20%u96C6%u5408%u4E0E%u6D41%u7684%u7EC3%u4E60%5D%0A%23%u96C6%u5408%u4E0EIO%u6D41%u7ED3%u5408%u7684%u7EC3%u4E60%0A%3E%u7EC3%u4E60%3A%0A%u6709%u4E94%u4E2A%u5B66%u751F%2C%u6BCF%u4E2A%u5B66%u751F%u67093%u95E8%u8BFE%u7A0B%3A%u6570%u5B66%2C%u8BED%u6587%2C%u82F1%u8BED%0A%u4ECE%u952E%u76D8%u8F93%u5165%u4EE5%u4E0A%u6570%u636E%28%u5305%u62EC%u59D3%u540D%2C%u4E09%u95E8%u8BFE%u6210%u7EE9%29%2C%0A%u8F93%u5165%u7684%u683C%u5F0F%3Azhangsan%2C%2030%2C%2040%2C%2040%u8BA1%u7B97%u51FA%u603B%u6210%u7EE9%2C%0A%u5E76%u628A%u5B66%u751F%u7684%u4FE1%u606F%u548C%u8BA1%u7B97%u51FA%u7684%u603B%u5206%u6570%u9AD8%u4F4E%u987A%u5E8F%u5B58%u653E%u5728%u78C1%u76D8%u6587%u4EF6%22stud.txt%22%u4E2D.%0A%0A1.%u63CF%u8FF0%u5B66%u751F%u5BF9%u8C61%3B%0A2.%u5B9A%u4E49%u4E00%u4E2A%u53EF%u64CD%u4F5C%u5B66%u751F%u5BF9%u8C61%u7684%u5DE5%u5177%u7C7B%3B%0A%0A**%u601D%u8DEF%3A**%0A1.%u901A%u8FC7%u83B7%u53D6%u952E%u76D8%u5F55%u5165%u7684%u4E00%u884C%u6570%u636E%2C%u5E76%u5C06%u5F55%u5165%u7684%u4FE1%u606F%u5C01%u88C5%u6210%u5B66%u751F%u5BF9%u8C61%3B%0A2.%u56E0%u4E3A%u5B66%u751F%u5BF9%u8C61%u5F88%u591A%2C%u4E14%u8981%u5BF9%u5B66%u751F%u7684%u603B%u5206%u6392%u5E8F%2C%u6240%u4EE5%u7528TreeSet%u96C6%u5408%3B%0A3.%u5C06%u96C6%u5408%u4E2D%u7684%u4FE1%u606F%u5199%u5165%u5230%u6587%u4EF6%u4E2D%3B%0A%0A---%0A%60%60%60java%0Aimport%20java.io.*%3B%0Aimport%20java.util.*%3B%0Aclass%20%20StudentInfoTest%0A%7B%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%20throws%20IOException%0A%09%7B%0A%09%09Comparator%3CStudent%3E%20comp%20%3D%20Collections.reverseOrder%28%29%3B//%u56E0%u4E3A%u4E4B%u524D%u7684%u6392%u5E8F%u662F%u4ECE%u5C0F%u5230%u5927%u6392%2C%u73B0%u5728%u4ECE%u5927%u5230%u5C0F%u6392%2C%u6240%u4EE5%u5B9A%u4E49%u6BD4%u8F83%u5668%0A%09%09Set%3CStudent%3E%20stus%20%3D%20StudentInfoTool.getStudent%28comp%29%3B%0A%09%09StudentInfoTool.writeToFile%28stus%29%3B%0A%09%7D%0A%7D%0Aclass%20Student%20implements%20Comparable%3CStudent%3E%0A%7B%0A%09private%20String%20name%3B%0A%09private%20int%20ma%2Ccn%2Cen%3B%0A%09private%20int%20sum%3B%0A%0A%09Student%28String%20name%2Cint%20ma%2Cint%20cn%2Cint%20en%29%0A%09%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%09this.ma%20%3D%20ma%3B%0A%09%09this.cn%20%3D%20cn%3B%0A%09%09this.en%20%3D%20en%3B%0A%09%09sum%20%3D%20ma%20+%20cn%20+%20en%3B%0A%09%7D%0A%09public%20String%20getName%28%29%0A%09%7B%0A%09%09return%20name%3B%0A%09%7D%0A%09public%20int%20getSum%28%29%0A%09%7B%0A%09%09return%20sum%3B%0A%09%7D%0A%09public%20String%20toString%28%29%0A%09%7B%0A%09%09return%20%22student%5B%22+name+%22%2C%20%22+ma+%22%2C%20%22+cn+%22%2C%20%22+en+%22%2C%5D%22%3B%0A%09%7D%0A%09public%20int%20compareTo%28Student%20stu%29%0A%09%7B%0A%09%09int%20num%20%3D%20new%20Integer%28this.sum%29.compareTo%28new%20Integer%28stu.sum%29%29%3B%0A%09%09if%20%28num%3D%3D0%29%0A%09%09%7B%0A%09%09%09return%20this.name.compareTo%28stu.name%29%3B%0A%09%09%7D%0A%09%09return%20num%3B%0A%09%7D%0A%09public%20int%20hashCode%28%29%0A%09%7B%0A%09%09return%20name.hashCode%28%29+sum*39%3B%0A%09%7D%0A%09public%20boolean%20equals%28Object%20obj%29%0A%09%7B%0A%09%09if%20%28%21%28obj%20instanceof%20Student%29%29%0A%09%09%7B%0A%09%09%09throw%20new%20ClassCastException%28%22%u4F20%u5165%u7C7B%u578B%u4E0D%u6B63%u786E%22%29%3B%0A%09%09%7D%0A%09%09Student%20stu%20%3D%20%28Student%29obj%3B%0A%09%09return%20this.name.equals%28stu.name%29%20%26%26%20this.sum%3D%3Dstu.sum%3B%0A%09%7D%0A%7D%0Aclass%20StudentInfoTool%0A%7B%0A%09public%20static%20Set%3CStudent%3E%20getStudent%28%29%20throws%20IOException%0A%09%7B%0A%09%09getStudent%28null%29%3B%0A%09%09return%20null%3B%0A%09%7D%0A%09public%20static%20Set%3CStudent%3E%20getStudent%28Comparator%3CStudent%3E%20comp%29%20throws%20IOException%0A%09%7B%0A%09%09BufferedReader%20bufr%20%3D%20new%20BufferedReader%28new%20InputStreamReader%28System.in%29%29%3B%0A%09%09String%20line%20%3D%20null%3B%0A%09%09Set%3CStudent%3E%20stus%20%3D%20null%3B%0A%09%09if%20%28comp%3D%3Dnull%29%0A%09%09%7B%0A%09%09%09stus%20%3D%20new%20TreeSet%3CStudent%3E%28%29%3B%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09stus%20%3D%20new%20TreeSet%3CStudent%3E%28comp%29%3B%0A%09%09%7D%0A%09%09while%20%28%28line%3Dbufr.readLine%28%29%29%20%21%3Dnull%29%0A%09%09%7B%0A%09%09%09if%20%28%22over%22.equals%28line%29%29%0A%09%09%09%7B%0A%09%09%09%09break%3B%0A%09%09%09%7D%0A%09%09%09String%5B%5D%20arr%20%3D%20line.split%28%22%2C%22%29%3B%0A%09%09%09Student%20stu%20%3D%20new%20Student%28arr%5B0%5D%2CInteger.parseInt%28arr%5B1%5D%29%2C%0A%09%09%09%09Integer.parseInt%28arr%5B2%5D%29%2C%0A%09%09%09%09Integer.parseInt%28arr%5B3%5D%29%29%3B%0A%09%09%09stus.add%28stu%29%3B%0A%09%09%7D%0A%09%09bufr.close%28%29%3B%0A%09%09return%20stus%3B%0A%09%7D%0A%09public%20static%20void%20writeToFile%28Set%3CStudent%3E%20stus%29%20throws%20IOException%0A%09%7B%0A%09%09BufferedWriter%20bufw%20%3D%20new%20BufferedWriter%28new%20FileWriter%28%22e%3A%5C%5Cstu.txt%22%29%29%3B%0A%09%09for%20%28Student%20stu%20%3A%20stus%29%0A%09%09%7B%0A%09%09%09bufw.write%28stu.toString%28%29%29%3B%0A%09%09%09bufw.write%28stu.getSum%28%29+%22%22%29%3B%0A%09%09%09bufw.newLine%28%29%3B%0A%09%09%09bufw.flush%28%29%3B%0A%09%09%7D%0A%09%09bufw.close%28%29%3B%0A%09%7D%0A%0A%7D%0A%0A%0A%60%60%60%0A

 

集合与IO流结合的练习