练习一:
package com.shujia.wyh.day25;
import java.io.*;
import java.util.Arrays;
public class IOTest1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\IdeaProjects\\bigdata17\\src\\com\\shujia\\wyh\\day25\\s.txt"));
String line = br.readLine();
byte[] bytes = line.getBytes();
Arrays.sort(bytes);
String s = new String(bytes);
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\IdeaProjects\\bigdata17\\src\\com\\shujia\\wyh\\day25\\ss.txt"));
bw.write(s);
bw.flush();
bw.close();
br.close();
}
}
练习二:
package com.shujia.wyh.day24;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class IOTest2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("io1.txt"));
ArrayList<String> list = new ArrayList<>();
String line = null;
while ((line=br.readLine())!=null){
list.add(line);
}
System.out.println(list);
br.close();
}
}
练习三:
package com.bigdat.java.day25;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class ListAndFileText1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o2.getAll()-o1.getAll();
int i1 = (i == 0)? o2.getChinese()-o1.getChinese():i;
int i2 = (i1 == 0)? o2.getMath()-o1.getMath():i1;
int i3 = (i2 == 0)? o2.getEnglish()-o1.getEnglish():i2;
return i3;
}
});
Student student = null;
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第"+i+"个学生的姓名:");
String name = scanner.next();
System.out.println("请输入该学生的语文成绩:");
int chinese = scanner.nextInt();
System.out.println("请输入该学生的数学成绩:");
int math = scanner.nextInt();
System.out.println("请输入该学生的英语成绩:");
int english = scanner.nextInt();
student = new Student(name,chinese,math,english);
ts.add(student);
System.out.println("-------------------------");
}
BufferedWriter br = null;
StringBuffer sb = null;
for (Student t : ts) {
String name = t.getName();
int chinese = t.getChinese();
int math = t.getMath();
int english = t.getEnglish();
sb = new StringBuffer();
sb.append("学生姓名:").append(name)
.append(",语文成绩:").append(chinese)
.append(",数学成绩:").append(math)
.append(",英语成绩:").append(english)
.append(",总成绩:").append(t.getAll());
String str = sb.toString();
try {
br = new BufferedWriter(new FileWriter("D:\\student.txt",true));
br.write(str);
br.newLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("计算成绩中,请稍后...");
}
}
学生类
package com.bigdat.java.day25;
public class Student {
private String name;
private int chinese;
private int math;
private int english;
private int all;
public Student() {
}
public Student(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getAll() {
return this.all = this.chinese+this.math+this.english;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?