我是靠谱客的博主 刻苦电灯胆,这篇文章主要介绍集合转文件(字符流),文本文件中的信息读取到新的集合中(学生年龄降序排列),现在分享给大家,希望可以做个参考。

学生类为例:

创建学生类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Objects; public class Student { private String name; private String sex; private int age; private String score; public Student() { } public Student(String name, String sex, int age, String score) { this.name = name; this.sex = sex; this.age = age; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", sex='" + sex + ''' + ", age='" + age + ''' + ", score='" + score + ''' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name) && Objects.equals(sex, student.sex) && Objects.equals(score, student.score); } @Override public int hashCode() { return Objects.hash(name, sex, age, score); } }
复制代码
1
创建Demo类代码示例:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package demo01; import java.io.*; import java.util.*; class AgeComparator implements Comparator<Student> { //比较器 Comparator,按照学生年龄降序(从大到小) //如果升序就o1.getage()-o2.getAge()(从小到大) @Override public int compare(Student o1, Student o2) { return o2.getAge() - o1.getAge(); } } public class Demo { public static void main(String[] args) throws IOException { ArrayList<Student> studentArrayList = new ArrayList<>(); Student student1 = new Student("刘德华", "男", 22, "89"); Student student2 = new Student("成龙", "男", 19, "99"); Student student3 = new Student("张三", "男", 18, "99"); Student student4 = new Student("李四", "男", 21, "99"); Student student5 = new Student("王五", "男", 25, "99"); studentArrayList.add(student1); studentArrayList.add(student2); studentArrayList.add(student3); studentArrayList.add(student4); studentArrayList.add(student5); Collections.sort(studentArrayList, new AgeComparato()); BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt")); for (Student s : studentArrayList ) { bw.write(s.getName() + "," + s.getSex() + "," + s.getAge() + "," + s.getScore()); bw.newLine(); bw.flush(); } System.out.println("写入成功"); BufferedReader br = new BufferedReader(new FileReader("student.txt")); ArrayList<String> arrayList = new ArrayList<>(); String line; while ((line = br.readLine()) != null) { arrayList.add(line); } br.close(); for (String s : arrayList ) { System.out.println(s); } } }

对于文件复制有以下几点需要注意:

1.优先选择字符缓冲流,字符流对于文本复制尤其包含中文汉字会效率很高,缓冲流又能间接增加传输效率;

2.字符流复制文本快的一部分原因是可以选择按行复制(优先于字节流复制文本);

3.比较器(Comparator)接口中的方法

int compare(T obj1, T obj2)  

比较o1和o2这两个对象如果: obj1 > obj2,这个方法返回正整数

obj2 == obj1, 返回0

obj1 < obj2,这个方法返回负整数

 

比较器案例

学生集合按照年龄和成绩排序

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Student { private String name; private int age; private int score; } //年龄比较器(从小到大) class AgeComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); //年龄由大到小排序 :return o2.getAge()-o1.getAge(); } } //成绩比较器 class ScoreComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return o1.getScore() - o2.getScore(); //成绩由大到小排序:return o2.getScore()-o1.getScore(); } } ​ public class Demo04Comparator { public static void main(String[] args) { //创建学生对象,添加到集合当中 ​ List<Student> students = new ArrayList<>(); ​ students.add(new Student("小龙",22,97)); ​ students.add(new Student("小明",15,86)); ​ students.add(new Student("小花",26,100)); ​ //按年龄从小到大对集合的学生对象进行排序 ​ Collections.sort(students, new AgeComparator()); System.out.println(students); ​ //按分数从低到高对集合中的学生对象进行排序进行排序 ​ Collections.sort(students, new ScoreComparator()); ​ System.out.println(students); ​ } ​ }

 

 

 

 

最后

以上就是刻苦电灯胆最近收集整理的关于集合转文件(字符流),文本文件中的信息读取到新的集合中(学生年龄降序排列)的全部内容,更多相关集合转文件(字符流),文本文件中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部