java程序题,希望各位大侠帮帮忙!!!
已知3个同学的姓名、年龄信息,请将这些信息写入student.txt文件中。(假设信息为:Mrk、18;Rose、19;Jak、17),然后再从文件中依次读取第二个同学、...
已知3个同学的姓名、年龄信息,请将这些信息写入student.txt文件中。(假设信息为:Mrk、18;Rose、19;Jak、17),然后再从文件中依次读取第二个同学、第一个同学、第3个同学信息并打印在屏幕上。
给位JAVA高手帮帮忙,我们没有学IO操作,但是老师都让我们先做题,做不来啊,谢谢各位啦~ 展开
给位JAVA高手帮帮忙,我们没有学IO操作,但是老师都让我们先做题,做不来啊,谢谢各位啦~ 展开
展开全部
刚学JAVA,代码不是那么完善,参考下吧
//RandomFileTest.java
import java.io.*;
public class RandomFileTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"Mrk",18);
Student s2=new Student(2,"Rose",19);
Student s3=new Student(3,"jak",17);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);
s.readStudent(raf);
s.readStudent(raf);
System.out.println(s.num+"name:"+s.name+" age:"+s.age);
raf.seek(0);
s.readStudent(raf);
System.out.println(s.num+"name:"+s.name+" age:"+s.age);
s.readStudent(raf);
s.readStudent(raf);
System.out.println(s.num+"name:"+s.name+" age:"+s.age);
raf.close();
}
}
class Student
{
int num;
String name;
int age;
public Student()
{
}
public Student(int num,String name,int age)
{
this.num=num;
this.name=name;
this.age=age;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeInt(age);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
age=raf.readInt();
}
}
//RandomFileTest.java
import java.io.*;
public class RandomFileTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"Mrk",18);
Student s2=new Student(2,"Rose",19);
Student s3=new Student(3,"jak",17);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);
s.readStudent(raf);
s.readStudent(raf);
System.out.println(s.num+"name:"+s.name+" age:"+s.age);
raf.seek(0);
s.readStudent(raf);
System.out.println(s.num+"name:"+s.name+" age:"+s.age);
s.readStudent(raf);
s.readStudent(raf);
System.out.println(s.num+"name:"+s.name+" age:"+s.age);
raf.close();
}
}
class Student
{
int num;
String name;
int age;
public Student()
{
}
public Student(int num,String name,int age)
{
this.num=num;
this.name=name;
this.age=age;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeInt(age);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
age=raf.readInt();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询