代码如下:
import java.io.*;
public class problem8
{
public static void main(String args[])
{
student stu=new student( " ", " ",0);
String str;
int operflag=0;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println( "选择操作类型: ");
System.out.println( "1表示输入学生信息: ");
System.out.println( "2表示显示学生信息 ");
try
{
str=buf.readLine();
operflag=Integer.parseInt(str);
}
catch(Exception e)
{
System.out.println( "操作出错,需要退出! ");
System.exit(0);
}
switch (operflag)
{
case 1:
try
{
RandomAccessFile f=new RandomAccessFile( "mytest.txt ", "rw ");
System.out.println( "请输入学生姓名: ");
stu.stu_name=buf.readLine();
System.out.println( "请输入学生学号: ");
stu.stu_No=buf.readLine();
System.out.println( "请输入学生成绩: ");
str=buf.readLine();
stu.stu_score=Integer.parseInt(str);
f.write(stu.stu_name.getBytes());
f.write(stu.stu_No.getBytes());
f.writeInt(stu.stu_score);
f.write( "\n ");
f.close();
System.out.println( "学生信息输入成功! ");