Java实验十

一、实验目的

  1. 掌握字节流的基本概念与用法
  2. 掌握字节流输入输出的用法。

实验内容:

必做

  1. 编写程序Equilateral_Triangle实现在控制台用“*”输出一个倒立的等边三角型,并写到文件triangle.txt中
  2. 完成第七章课后习题第4题,注意要处理好当输入的不满足条件的情况的处理:不是整数、不是0-100的数。
package ex10;
import java.io.*;
public class Equilateral_Triangle {
	
	//倒三角行
	//public static String triangle() {
		
	
	public static void main(String[] args) throws Exception{
		//String a = "*";
		FileWriter fos = new FileWriter("D:\\eclipse-workspace\\15180125yangjiaxuan\\src\\ex10\\triangle.txt");
		for(int i=8;i>0;i--){
			for (int j = 0; j < 2*(i)-1; j++) {
				System.out.print("*");
				fos.write("*");
			}
			//System.out.print(" ");
			System.out.println();
			fos.write("\r\n");
			for (int k = 9;k>i;k--) {
				System.out.print(" ");
				fos.write(" ");
			}
		}
		
		fos.flush();
		
		fos.close();
		
		//下面都是多余的
		//return triangle();
	
		
		//FileOutputStream fos = null;
		//String msg = triangle();
		//System.out.println( );
		/*
		try {
			//创建字节流
			fos = new FileOutputStream("triangle");
			
			byte [] bytes = msg.getBytes();
			
			fos.write(bytes);
			
			fos.flush();
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(fos != null) {
				try {
					fos.close();
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		*/
	}
}

package ex10;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;

public class StudentClass {
	// 改成你自己的文件路劲
	private static final String FILE_PATH= "D:\\ex10\\data.txt";
	
	static double [] student = new double [5];
	
	public static void Score() {
		
		Scanner s = new Scanner(System.in);
		
		for(int i = 1 ;i <= 5;i++) {
			
			System.out.println("请输入第"+i+"位学生的成绩(0到100之间的数): ");
			
			double a = s.nextDouble();
			
			
			if(a<0||a>100) {
				
				System.out.println("对不起,您的输入有误!");
				
				break;
			}
			
			student[i-1] = a;
			
		}
		s.close();
	}
	
	public static void writer() {
		FileOutputStream fos = null;
		try {
			
				fos = new FileOutputStream(FILE_PATH);
				
				for(int i = 0 ;i < 5;i++) {
					String temp = Double.toString(student[i]);
					temp += " ";
					fos.write(temp.getBytes());
					
				}
				fos.flush();
		}catch(Exception e ) {
			e.printStackTrace();
		}finally {
			if(fos != null) {
				try {
					fos.close();
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public static void Input() {
		double [] b = new double[5] ;
		BufferedReader bf = null;// 这里选择用带缓存的读取方式,可以避免单位数而出现空指针异常的现象
		String[] a = new String[5]; 
		try {
			bf = new BufferedReader(new FileReader(FILE_PATH));
			String line = "", line1;
	
			while((line1=bf.readLine()) != null){
				line += line1;
			}
			a = line.split(" ");
			for(int i = 0;i<5;i++) {
				
				
				
				b[i] = Double.parseDouble(a[i]);
			}
			
			Arrays.sort(b);//java 中自带的排序方法
			
			for(double i : b) {
				System.out.println(i);
			}
			double averge = (b[0]+b[1]+b[2]+b[3]+b[4])/5;
			
			System.out.println("平均分为: "+averge);
			
			}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(bf != null) {
				try {
					bf.close();
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}

	public static void main(String[] args) {
		Score();
		writer();
		System.out.println("------------------------------------"+'\n'+"由小到大排序:");
		Input();

	}

}


运行结果

在这里插入图片描述

实验总结:

学习了文件字节流的输入的使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值