流操作规律

流操作规律

1.明确源和目的;
    源:输入流。InputStream Reader
    目的:输出流。OutputStream Writer
2.操作的数据是不是纯文本。
    是:字符流
    否:字节流
3.当体系明确后,再明确要使用哪个具体的对象。
    通过设备来区分:
      源设备:内存,硬盘,键盘
      目的设备:内存,硬盘,控制台


例子:建立一个名为student.txt的文件用于存放学生的数据(包含学生学号、姓名、数学、语文、英语、平均成绩、总成绩),
  从键盘输入5个学生的学号、姓名及三门课的成绩,计算出总分和平均分存入文件student.txt,并在控制台显示该文件的内容。


分析:
    源:键盘,所以应该用System.in, 想要提高效率,所以用BufferedReader,因为BufferedReader是字符流对象,System.in是字节流对象,
    所以要用到转换流InputStreamReader。接收键盘输入,像下面这样写:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    目的:student.txt文件,是一个文本文件,所以用,FileInputStream,想要提高效率,所以要用BufferedWriter,同样也要用到转换流OutputStreamWriter。

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream());

package it.practise.com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ScoreCount {

	public static void main(String[] args) {
		try {
			new ScoreCount().getStudentInfo();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void getStudentInfo() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("G:\\demo\\student.txt")));
		String line = null;
		String[] sts = null;
		StudentInfo stu = null;
		while((line = br.readLine()) != null) {
			//定义一个结束标记
			if("over".equals(line))
				break;
			else
			{
				sts = line.split(" ");
				int id = Integer.parseInt(sts[0]);
				String name = sts[1];
				int mathScore = Integer.parseInt(sts[2]);
				int chineseScore = Integer.parseInt(sts[3]);
				int englishScore = Integer.parseInt(sts[4]);
				int totalScore = mathScore + chineseScore + englishScore;
				int avergeScore = totalScore/3;
				stu = new StudentInfo(id, name, mathScore, chineseScore, englishScore, avergeScore, totalScore);
				
				bw.write(stu.toString());
				bw.newLine();
				bw.flush();
			}
		}
		
		br.close();
		bw.close();
	}
	
}

class StudentInfo {
	private int id;
	private String name;
	private int mathScore;
	private int chineseScore;
	private int englishScore;
	private int avergeScore;
	private int totalScore;
	
	StudentInfo(int id, String name, int mathScore, int chineseScore, int englishScore, int avergeScore, int totalScore) {
		this.id = id;
		this.name = name;
		this.mathScore = mathScore;
		this.chineseScore = chineseScore;
		this.englishScore = englishScore;
		this.avergeScore = avergeScore;
		this.totalScore = totalScore;
	}
	
	public String toString() {
		return String.valueOf(id) + " " + name + " " + mathScore + " " + chineseScore + " " + englishScore + " " + avergeScore + " " + totalScore;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值