Java 流练习:从csv文件读取学生成绩,计算综合成绩并输出到文件
package project005;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
class Student
{
private String id;
private String name;
private float PingShiScore;
private float ShiYanScore;
private float QiMoScore;
private Integer ZongHeScore;
public Student() {
super();
}
public Student(String id, String name, float PingShiScore, float ShiYanScore, float QiMoScore, int ZongHeScore)
{
super();
this.id = id;
this.name = name;
this.PingShiScore = PingShiScore;
this.ShiYanScore = ShiYanScore;
this.QiMoScore = QiMoScore;
this.ZongHeScore = ZongHeScore;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPingShiScore() {
return PingShiScore;
}
public void setPingShiScore(float PingShiScore) {
this.PingShiScore = PingShiScore;
}
public float getShiYanScore() {
return ShiYanScore;
}
public void setShiYanScore(float ShiYanScore) {
this.ShiYanScore = ShiYanScore;
}
public float getQiMoScore() {
return QiMoScore;
}
public void setQiMoScore(float QiMoScore) {
this.QiMoScore = QiMoScore;
}
public int getZongHeScore() {
return ZongHeScore;
}
public void setZongHeScore(int ZongHeScore) {
this.ZongHeScore = ZongHeScore;
}
@Override
public String toString() {
return "Student [id=" + id
+ ", name=" + name
+ ", PingShiScore=" + PingShiScore
+ ", ShiYanScore=" + ShiYanScore
+ ", QiMoScore=" + QiMoScore
+ ", ZongHeScore=" + ZongHeScore + "]";
}
}
public class CalculateAndArrange
{
public static void main(String[] args)
{
File fname = new File("某课程成绩表.csv");
InputStreamReader fr = null;
BufferedReader br = null;
try {
fr = new InputStreamReader(new FileInputStream(fname));
br = new BufferedReader(fr);
String rec = null;
String[] argsArr = null;
String [][]s = new String[42][6];
int count = 0;
while ((rec = br.readLine()) != null) {
argsArr = rec.split(",");
for (int i = 0; i < argsArr.length; i++)
{
s[count][i] = argsArr[i];
}
count++;
}
Student[] stu = new Student[50];
for(int i = 1; i < count; i++)
{
stu[i] = new Student();
stu[i].setId(s[i][0]);
stu[i].setName(s[i][1]);
stu[i].setPingShiScore(Float.parseFloat(s[i][2]));
stu[i].setShiYanScore(Float.parseFloat(s[i][3]));
stu[i].setQiMoScore(Float.parseFloat(s[i][4]));
stu[i].setZongHeScore((int) Math.round(stu[i].getPingShiScore() * 0.2 + stu[i].getShiYanScore() * 0.3 + stu[i].getQiMoScore() * 0.5));
}
for(int i = 1; i < count; i++)
{
System.out.println(stu[i]);
}
for(int i = 1; i < count; i++)
{
File f=new File(stu[i].getId() + stu[i].getName() + ".txt");
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter dos = new OutputStreamWriter(fos);
dos.write("学号:" + stu[i].getId() + "\r\n");
dos.write("姓名:" + stu[i].getName() + "\r\n");
dos.write("平时成绩:" + (int) stu[i].getPingShiScore() + "\r\n");
dos.write("实验成绩:" + (int) stu[i].getShiYanScore() + "\r\n");
dos.write("期末成绩:" + (int) stu[i].getQiMoScore() + "\r\n");
dos.write("综合成绩:" + (int) stu[i].getZongHeScore() + "\r\n");
dos.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fr != null)
fr.close();
if (br != null)
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}