1.分析程序的输出结果:
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){}
}
}
ABCDEFG
ABCDEFGEF
2.从键盘输入n(n值由键盘输入,n>2)个学生的JAVA课程成绩,并将成绩写入到d:\javagrade.txt文件中。然后,从该文件中找出最高分和最低分并输出到屏幕。
import java.io.*;
import java.util.Scanner;
public class test {
public static void main(String args[]) throws IOException {
String fileName="d:\\javagrade.txt",line;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BufferedWriter writeout = new BufferedWriter(new FileWriter (fileName));
Scanner read =new Scanner(System.in);
String s;
for(int i=0;i<n;i++){
s=read.nextLine();
writeout.write(s);
writeout.newLine();
}
writeout.close();
BufferedReader in=new BufferedReader(new FileReader(fileName));
line =in.readLine();
int max=0,i=0,min=1000;
while(line!=null){
if(Integer.parseInt(line)>max){
max=Integer.parseInt(line);
}
if(Integer.parseInt(line)<min){
min=Integer.parseInt(line);
}
i++;
line=in.readLine();
}
in.close();
System.out.printf("最高分为%d\n",max);
System.out.printf("最低分为%d\n",min);
}
}
3.分析成绩单
现在有如下格式的成绩单(文本格式)score.txt:
姓名:张三,数学77分,物理67分,英语70分。
姓名:李四,数学82分,物理90分,英语85分。
姓名:王五,数学80分,物理85分,英语75分。
要求按行读取成绩单,统计每个学生的总成绩,并写入totalscore.txt文件中,总成绩的格式为:
张三,总分214分。
李四,总分257分。
王五,总分240分。
package test;
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;
public class test {
public static void main(String args[]) throws IOException {
String fileName="d:\\score.txt",line;
String fileName2="d:\\totalscore.txt";
BufferedReader in=new BufferedReader(new FileReader(fileName));
line =in.readLine();
String regex ="\\D+";
int i=0,j=1;
int[] total ={0,0,0};
String[] name=new String[3];
while(line!=null){
//匹配提取姓名
Matcher m1 = Pattern.compile(":.*,数学").matcher(line);
while (m1.find()) {
name[i]=m1.group().substring(1,3);
}
//提取分数
String digit[]=line.split(regex);
j=1;
while(j<4){
total[i]+=Integer.parseInt(digit[j]);
j++;
}
i++;
line=in.readLine();
}
in.close();
//将总分写入文件
BufferedWriter writeout = new BufferedWriter(new FileWriter (fileName2));
for(int k=0;k<3;k++){
writeout.write(name[k]);
writeout.write(",总分");
writeout.write(String.valueOf(total[k]));
writeout.write("分。");
writeout.newLine();
}
writeout.close();
}
}
4.在d:\logo.png文件中,保存的是中国石油大学(华东)的Logo图片。读取该图片文件中的第n(n值由键盘输入)个字节输出到屏幕。
5.综合应用:
在C:\newFile文件夹下存放有两类文件:.txt文本文件和.jpg图片文件。现在需要将C:\newFile文件夹中的.txt文件中的内容读出并显示到屏幕,将C:\newFile文件夹中的.jpg图片文件复制到D:\newFile文件夹中。然后删除C:\newFile文件夹中的.jpg图片文件。
提示:通过BufferedReader读文本文件;
通过BufferedInputStream和BufferedOutputStream对象读写图片文件;
通过File类的delete()方法删除文件。