Java输入输出流练习:从csv文件读取学生成绩,计算综合成绩并输出到文件

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();  
		}  
	}  
}

}


  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值