Java实验六:输入输出流

一、分析程序的输出结果

import java.io.*;

public class E {
public static void main(String args[]) {

    try{

        FileOutputStream out=new FileOutputStream("hello.txt");
        FileInputStream in=new FileInputStream("hello.txt");
        byte content[]="ABCDEFG".getBytes();
        StringBuffer bufferOne=new StringBuffer();
        StringBuffer bufferTwo=new StringBuffer();
        int m=-1;
        byte tom[]=new byte[3];
        out.write(content);
        out.close();

        while((m=in.read(tom,0,3))!=-1){
            String s1=new String(tom,0,m);
            bufferOne.append(s1);
            String s2=new String(tom,0,3);
            bufferTwo.append(s2);
        }

        in.close();
        System.out.printf("%s\n",bufferOne);
        System.out.printf("%s\n",bufferTwo);
    }

    catch(IOException e){}
}
}

二、读写文本文件

从键盘输入n(n值由键盘输入,n>2)个学生的JAVA课程成绩,并将成绩写入到d:\javagrade.txt文件中。然后,从该文件中找出最高分和最低分并输出到屏幕。

import java.io.*;
import java.util.Scanner;
import java.util.regex.*;
import java.util.Arrays;

public class text {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
        //从键盘输入部分
		System.out.print("请输入学生个数:"+"\n");
		Scanner reader1 = new Scanner(System.in);
		int n = reader1.nextInt();	
		System.out.print("请输入学生成绩:"+"\n");
		Scanner reader2 = new Scanner(System.in);
		String score = reader2.nextLine();
		reader1.close();
		reader2.close();

		//把成绩写入文件
		String file = "d:\\javagrade.txt";
		FileWriter writer=new FileWriter(file);
		writer.write(score);
		writer.close();
		
		//读取文件
		BufferedReader reader3=new BufferedReader(new FileReader(file));
		String ReadSccore = reader3.readLine();
		reader3.close();
		
		//分离n个分数并保存到数组s[]中
		String regex = "[0-9.]+";
		Pattern p = Pattern.compile(regex); //模式串
		Matcher m = p.matcher(ReadSccore); //匹配对象
		
		int s[] = new int[n];
		int i = 0;
		while(m.find()){
			//将字符串参数作为有符号的十进制整数进行解析
			s[i] = Integer.parseInt(m.group()); //返回符合正则表达式的字符串
			i++;
		}
		
		//数组元素升序排序
		Arrays.sort(s);
		
		//输出
		System.out.print("最高分:"+s[s.length - 1]+"\n");
		System.out.print("最低分:"+s[0]);
			
	}

}

运行结果:
在这里插入图片描述

三、分析成绩单

现在有如下格式的成绩单(文本格式)score.txt:
姓名:张三,数学77分,物理67分,英语70分。
姓名:李四,数学82分,物理90分,英语85分。
姓名:王五,数学80分,物理85分,英语75分。
要求按行读取成绩单,统计每个学生的总成绩,并写入totalscore.txt文件中,总成绩的格式为:
张三,总分214分。
李四,总分257分。
王五,总分240分。

import java.io.*;
import java.util.Scanner;
import java.util.regex.*;


public class text {

	public static void main(String[] args) throws IOException {
	
		//按行读取文本文件
		InputStreamReader isr = new InputStreamReader(new FileInputStream("d://score.txt"), "UTF-8");   
		BufferedReader reader = new BufferedReader(isr);   
		String line = reader.readLine();
		//reader.close();
		//System.out.print(line);//输出一下看看ReadSccore是否为乱码。
		
		int n=0;
		String name[]=new String[10]; //学生姓名
		int personalSubScore[]=new int[3]; //一个学生的3门课的成绩
		int personalSumScore[]=new int[10]; //所有学生的个人总成绩,假设学生数少于10个。
		
		
		while(line!=null){
			
			//分离成绩
			String regex1="[0-9.]+";
			Pattern p = Pattern.compile(regex1); //模式串
			Matcher m = p.matcher(line); //匹配对象
			int i=0;
			int SumScore=0;
			while(m.find()){
				//完成了一行信息的处理:分离了一个学生的单科成绩,统计了一个学生的总成绩。
				personalSubScore[i]=Integer.parseInt(m.group());
				SumScore = SumScore+personalSubScore[i];
				personalSumScore[n]=SumScore;
				i++;
			}
			
			
			//分离姓名
			String regex2 = ":.+?,";
			Pattern p2 = Pattern.compile(regex2);
			Matcher m2 = p2.matcher(line);			
			StringBuffer s3 = new StringBuffer();
			while(m2.find()){
				s3.append(m2.group());
			}
			
			String regex3 = "[\u4e00-\u9fa5]+";
			Pattern p3 = Pattern.compile(regex3);
			Matcher m3 = p3.matcher(s3);  //张三
			StringBuffer subname = new StringBuffer();
			while(m3.find()){
				subname.append(m3.group());
				name[n] = subname.toString();
			}
            
			//接着读下一行
			line = reader.readLine();
			n++;
			
		}
		
		reader.close();
		
		String tofile = "d://totalscore.txt";
		BufferedWriter to = new BufferedWriter(new FileWriter(tofile));
		for(int i = 0; i < n; i++){
			to.write(name[i]+",总分"+personalSumScore[i]+"分");
			to.newLine();
		}
		to.close();

	}

}

四、随机流读取二进制文件

在d:\logo.png文件中,保存的是中国石油大学(华东)的Logo图片。读取该图片文件中的第n(n值由键盘输入)个字节输出到屏幕。
在这里插入图片描述

import java.util.Scanner;
import java.io.*;

public class mymain6_4 {
	public static void main(String args[]) throws Exception{		
		String filename = "d:\\logo.png";
		RandomAccessFile out = new RandomAccessFile(filename, "r");
		long logolength = out.length();
		System.out.print("请输入一个0~"+logolength+"的整数:");
		Scanner reader = new Scanner(System.in);
		long n = reader.nextLong();
		reader.close();
		out.seek(n);
		System.out.print("该图片的第"+n+"个字节是:"+out.readByte());
		out.close();
	}
}

五、综合应用

在C:\newFile文件夹下存放有两类文件:.txt文本文件和.jpg图片文件。现在需要将C:\newFile文件夹中的.txt文件中的内容读出并显示到屏幕,将C:\newFile文件夹中的.jpg图片文件复制到D:\newFile文件夹中。然后删除C:\newFile文件夹中的.jpg图片文件。

提示:通过BufferedReader读文本文件;
通过BufferedInputStream和BufferedOutputStream对象读写图片文件;
通过File类的delete()方法删除文件。

import java.io.*;

public class mymain6_5 {
	public static void main(String args[]) throws Exception{
		//提前在C盘和D盘建好newFile文件夹,提前在C盘的newFile文件夹里建好.txt文件和.jpg文件
		File file = new File("c:\\newFile");
		File[] filename = file.listFiles();//目录
		
		for(int i = 0; i < filename.length; i++){			
			if(filename[i].getName().endsWith(".txt")){
				//读取txt文件
				BufferedReader cfile = new BufferedReader(new FileReader(file+"\\"+filename[i].getName()));
				String line = cfile.readLine();
				while(line != null){
					System.out.print(line);
					line = cfile.readLine();
				}
				cfile.close();
			}
			
			else{
				//读写jpg文件
				BufferedInputStream cjpgfile = new BufferedInputStream(new FileInputStream(file+"\\"+filename[i].getName()));
				BufferedOutputStream djpgfile = new BufferedOutputStream(new FileOutputStream("d:\\newFile\\"+filename[i].getName()));
				int reader = cjpgfile.read();
				while(reader != -1){
					djpgfile.write(reader);
					reader = cjpgfile.read();			
				}
				cjpgfile.close();
				djpgfile.close();
			}
		}
				
				
		//删除jpg文件
		for(int i = 0; i < filename.length; i++){
			if(filename[i].getName().endsWith(".jpg")){
				filename[i].delete();
			}
		}
	}
}

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值