集合与io流结合的练习

author author     2022-09-06     560

关键词:

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

 

properties和io流结合的方法练习(代码片段)

...assPropertiesDemo03publicstaticvoidmain(String[]args)throwsIOException//把集合中的数据保存到文件myStore();privatestaticvoidmyStore()throwsIOException//voidstore(Writerwriter,Stringcomments)将此属性列表(键和元素对)写入此Properties表中Propertiesprop=newProperties();prop... 查看详情

javase-19.1.1io流练习案例-集合到文件(排序改进版)

1packageday10.lesson1.p1;23importjava.io.BufferedWriter;4importjava.io.FileWriter;5importjava.io.IOException;6importjava.util.Comparator;7importjava.util.Scanner;8importjava.util.TreeSet;910/*111IO流练习 查看详情

io流--与properties集合配合使用(代码片段)

IO流--与properties集合配合使用:注:生产上主要用于常量文件的配置,读取常量文件;1:properties集合的放值与取值:/**properties集合继承自hashTable,使用properties父类的放值(put();),取值(get();)*功能,遍历集合得到的是Object类... 查看详情

javaio流相关代码(properties类的常见方法与应用)(代码片段)

Properties类Properties是hashtable的子类。也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。是集合中和IO技术相结合的集合容器。该对象的特点:可以用于键值对形式的配置文件。那么在加载数据时,需要... 查看详情

io流27-properties集合

packagecn.itcast.demo;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Properties;importjava.util.Set;/**集合对象Properties类,继承Hashtable,实现Map接口*可以和IO对象结合使用,实现数据 查看详情

io流相关知识(file,字节流,字符流,特殊操作流(标准输入流,标准输出流,对象序列化与反序列化,properties与io流结合))相关知识总结

...输出流,对象序列化与反序列化,properties与IO流结合))相关知识总结文章目录IO流相关知识(File,字节流,字符流,特殊操 查看详情

java之stream流(代码片段)

本文主要介绍了Stream流,包括集合和数组的Stream流创建方式,List和Set集合直接使用stream()方法创建,Map集合间接使用stream()方法创建,而数组使用Stream流的静态方法of()创建,介绍了Stream流的中间操作,包括过滤f... 查看详情

集合练习题(代码片段)

模拟斗地主挑战介绍扑克牌都是由"♠","♥","♣","♦"4种花色和"2,3,4,···,A,J,Q,K,"13种数字组合形成的52张正牌以及"大王"和"小王"两张副牌组成。本次挑战将结合本章所学知识模拟斗地主的... 查看详情

集合练习题(代码片段)

模拟斗地主挑战介绍扑克牌都是由"♠","♥","♣","♦"4种花色和"2,3,4,···,A,J,Q,K,"13种数字组合形成的52张正牌以及"大王"和"小王"两张副牌组成。本次挑战将结合本章所学知识模拟斗地主的... 查看详情

16io(properties序列化流打印流commonsio)(代码片段)

Properties集合的特点*A:Properties集合的特点*a:Properties类介绍*Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串*b:特点*Hashtable的子类,map集合中的方法都可以用... 查看详情

练习:序列化集合(代码片段)

...Stream;23importjava.io.*;4importjava.util.ArrayList;56/*7练习:序列化集合8当我们想在文件中保存多个对象的时候9可以把多个对象存储到一个集合中10对集合进序列化和反序列化11分析:121.定义一个存储Person对象的ArrayList集合132.往ArrayList集合... 查看详情

01变量与数据类型

...法5天基础语法练习强化1天面向对象1天常用API(字符串,集合,IO流)3天常用API练习强化1天就业班前期讲解JavaSE的进阶内容01.02_Java语言发展史和平台概述詹姆斯·高斯林(JamesGosling)SUN(StanfordUniversityNetwork,斯坦福大学网络公司... 查看详情

python的集合与字典练习

  集合与字典练习  question1  问题描述:有一个列表,其中包括10个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最后,然后输出这个列表。最终样式是[2,3,4... 查看详情

一个java基础练习

...里,下次须要的朋友能够来这里看。用到知识点:数组、集合、IO流问题描写叙述:在例如以下图所看到的的一个txt文件里读取数据到内存,然后统计列除过0的各个数字的个数(放入Map)并依照列的数据大小排序。代码:packageco... 查看详情

javase-19.1.3io流练习案例-复制多级文件夹

1packageday10.lesson1.p3;23importjava.io.*;45/*61.3案例-复制多级文件夹781.创建数据源File对象92.创建目的地File对象103.写方法实现文件夹的复制,参数为数据源File对象和目的地File对象114.判断数据源File是否是文件12是文件:13直接复制,用... 查看详情

02025_字符流练习

1、复制文件  原理:读取一个已有的数据,并将这些读到的数据写入到另一个文件。2、代码:1importjava.io.File;2importjava.io.FileInputStream;3importjava.io.FileOutputStream;4importjava.io.IOException;56publicclassCopyFileTest{7publicstaticvoidmai 查看详情

02026_字符流练习

1、练习:复制文本文件2、思路:  (1)既然是文本涉及编码表。需要用字符流;  (2)操作的是文件。涉及硬盘;  (3)有指定码表吗?没有,默认就行。1importjava.io.FileReader;2importjava.io.FileWriter;3importjava.io.IOException;45p... 查看详情

javascript数据结构与算法-集合练习

集合的实现functionSet(){this.dataStore=[];this.add=add;this.remove=remove;this.size=size;this.union=union;this.intersect=intersect;this.subset=subset;this.difference=difference;this.show=show;this.contains 查看详情