public class StuScore { //学生分数类
String name;
int[] record = new int[4]; //分数数组
int total; //总分
float avg; //平均分
String grade; //学分
//定义构造函数
//get()与set()方法的定义
//定义返回总分,平均分,学分的方法
}
运行结果示例
请顺序输入名字及语文,数学,英语,科学的成绩。(eof: 输入完毕)
啸鸣 66 77 88 67
洪姬 88 96 77 66
庞龙 77 55 87 57
eof文件内容
Student型对象1(名字,语文,数学,英语,科学,总分,平均分,学分,)
Student型对象2(名字,语文,数学,英语,科学,总分,平均分,学分,)
Student型对象3(名字,语文,数学,英语,科学,总分,平均分,学分,)
///////////////////////代码///////////////////////////////////////////
public static void main(String[] args) throws IOException {
System.out.println("请顺序输入名字及语文,数学,英语,科学的成绩。(eof: 输入完毕)");
int stucount = 0;
String path = "c:\\stuscore.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
String data = null;
while(true){
data = br.readLine();
if(data.equals("eof")){
break;
}
bw.write(data);
stucount++;
bw.newLine();
}
bw.close();
RandomAccessFile raf = new RandomAccessFile(new File(path), "rw");
StuScore[] stu = new StuScore[stucount];
raf.seek(0);
for(int i=0;i<stucount-1;i++){
stu[i].name = raf.readUTF(); //我的程序到这里就报错了。java.io.EOFException
stu[i].record[0] = raf.readInt();
stu[i].record[1] = raf.readInt();
stu[i].record[2] = raf.readInt();
stu[i].record[3] = raf.readInt();
}
raf.close();
System.out.println(stu[0].name);
}
我还是不太了解 RandomAccessFile 类读取文件的机制。。不知道是否应该用它, 请各位指教,谢谢啦。。
String name;
int[] record = new int[4]; //分数数组
int total; //总分
float avg; //平均分
String grade; //学分
//定义构造函数
//get()与set()方法的定义
//定义返回总分,平均分,学分的方法
}
运行结果示例
请顺序输入名字及语文,数学,英语,科学的成绩。(eof: 输入完毕)
啸鸣 66 77 88 67
洪姬 88 96 77 66
庞龙 77 55 87 57
eof文件内容
Student型对象1(名字,语文,数学,英语,科学,总分,平均分,学分,)
Student型对象2(名字,语文,数学,英语,科学,总分,平均分,学分,)
Student型对象3(名字,语文,数学,英语,科学,总分,平均分,学分,)
///////////////////////代码///////////////////////////////////////////
public static void main(String[] args) throws IOException {
System.out.println("请顺序输入名字及语文,数学,英语,科学的成绩。(eof: 输入完毕)");
int stucount = 0;
String path = "c:\\stuscore.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
String data = null;
while(true){
data = br.readLine();
if(data.equals("eof")){
break;
}
bw.write(data);
stucount++;
bw.newLine();
}
bw.close();
RandomAccessFile raf = new RandomAccessFile(new File(path), "rw");
StuScore[] stu = new StuScore[stucount];
raf.seek(0);
for(int i=0;i<stucount-1;i++){
stu[i].name = raf.readUTF(); //我的程序到这里就报错了。java.io.EOFException
stu[i].record[0] = raf.readInt();
stu[i].record[1] = raf.readInt();
stu[i].record[2] = raf.readInt();
stu[i].record[3] = raf.readInt();
}
raf.close();
System.out.println(stu[0].name);
}
我还是不太了解 RandomAccessFile 类读取文件的机制。。不知道是否应该用它, 请各位指教,谢谢啦。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货