实验七字符串和日期类
一、实验目的:掌握字符输入、输出流用法。
二、实验要求:
1.按如下描述,编程实现:
现在有如下格式的成绩单(文本格式)score.txt:
姓名:张三,数学72分,物理67分,英语70分。
姓名:李四,数学92分,物理98分,英语88分。
姓名:王五,数学68分,物理80分,英语77分。
要求按行读取成绩单,并在该行的末尾加上该同学的总成绩,然后再将该行写入到一个名字为scoreAnalysis.txt的文本中。
提示:通过BufferedReader和BufferedWriter实现读写文本文件;
通过Scanner对象分析出字符串中的数据。
2.分析程序的输出结果:
import java.io.*;
public class E {
public static void main(String args[]) {
try{
FileOutputStream out=new FileOutputSteam(“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.println(bufferOne);
System.out.println(bufferTwo);
}
catch(IOException e){}
}
}
3.综合应用(选做):
在C:\newFile文件夹下存放有两类文件:.txt文本文件和.jpg图片文件。
现在需要将C:\newFile文件夹中的.txt文件中的内容读出并显示到屏幕,将C:\newFile文件夹中的.jpg 图片文件复制到D:\newFile文件夹中。
然后删除C:\newFile文件夹中的.jpg图片文件。
提示:通过BufferedReader读文本文件;
通过BufferedInputStream和BufferedOutputStream对象读写图片文件;
通过File类的delete()方法删除文件。