1 Star 0 Fork 0

Blue/javaweb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.txt 5.24 KB
一键复制 编辑 原始数据 按行查看 历史
Blue 提交于 2022-10-17 23:53 +08:00 . rename project-1.txt to test.txt.
Student类代码:
//定义一个Studnet类,该类包含“学号”,“姓名”,“数学成绩”,“语文成绩”,“外语成绩” 这几个数据字段。
public class Student {
private String id;
private String name;
private String math;
private String chinese;
private String english;
public Student(){}
public Student(String id,String name,String math,String chinese,String english){
}
public void setId(String id){
this.id=id;
}
public String getId(){
return this.id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setMath(String math){
this.math=math;
}
public String getMath() { return this.math;}
public void setChinese(String chinese){
this.chinese=chinese;
}
public String getChinese() { return this.chinese;}
public void setEnglish(String english){
this.english=english;
}
public String getEnglish() { return this.english;}
public String toString() {
return String.format("ID: %s, Name: %s, Math:%s, Chinese:%s, English:%s", this.getId(), this.getName(),this.getMath(),this.getChinese(),this.getEnglish());
}
}
StudentManager类代码:
import java.io.*;
import java.nio.charset.Charset;
import java.io.FileInputStream;
public class StudentManager {
public static void main(String[] args) throws IOException {
StudentManager main = new StudentManager();
String filePath1 = "C:\\students.txt";
main.readStudentFromFile(filePath1);//学号和姓名
String filePath2 = "C:\\math.txt";
main.readmathFromFile(filePath2);//数学
String filepath3 = "C:\\chinese.txt";
main.readchineseFromFile(filepath3);//语文
String filepath4 = "C:\\english.txt";
main.readenglishFromFile(filepath4);//英语
}
public void readStudentFromFile(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
InputStreamReader inputSteamreader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputSteamreader);
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) continue;
Student student = this.createStudent(line);
System.out.println(student.toString());
}
fileInputStream.close();
}
private Student createStudent(String line) {
String[] vals = line.split("\t");//拆分字符串
Student student = new Student();
student.setId(vals[0]);
student.setName(vals[1]);
return student;
}
//读入数学成绩
public void readmathFromFile(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
InputStreamReader inputSteamreader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputSteamreader);
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) continue;
Student student = this.createmath(line);
System.out.println(student.toString());
}
fileInputStream.close();
}
private Student createmath(String line) {
String[] vals = line.split("\t");//拆分字符串
// Student student = new Student();
student.setMath(vals[1]);
return student;
}
//读入语文成绩
public void readchineseFromFile(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
InputStreamReader inputSteamreader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputSteamreader);
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) continue;
Student student = this.createchinese(line);
System.out.println(student.toString());
}
fileInputStream.close();
}
private Student createchinese(String line) {
String[] vals = line.split("\t");//拆分字符串
Student student = new Student();
student.setChinese(vals[1]);
return student;
}
//读入英语成绩
public void readenglishFromFile(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
InputStreamReader inputSteamreader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputSteamreader);
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) continue;
Student student = this.createenglish(line);
System.out.println(student.toString());
}
fileInputStream.close();
}
private Student createenglish(String line) {
String[] vals = line.split("\t");//拆分字符串
Student student = new Student();
student.setEnglish(vals[1]);
return student;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/blueLANSHUNLI/javaweb.git
git@gitee.com:blueLANSHUNLI/javaweb.git
blueLANSHUNLI
javaweb
javaweb
master

搜索帮助