import java.io.*;class RandomFileTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"zhangsan",98.5);
Student s2=new Student(2,"lisi",96.5);
Student s3=new Student(3,"wangwu",78.5);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);
for(long i=0;i<raf.length();i=raf.getFilePointer())
{
s.readStudent(raf);
System.out.println(s.num+":"+s.name+":"+s.score);
}
raf.close();
}
}class Student
{
int num;
String name;
double score;
public Student()
{
}
public Student(int num,String name,double score)
{
this.num=num;
this.name=name;
this.score=score;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
}
}
这个是一个简单的程序,没有什么大的问题,有个地方我不明白就是在自己定义的类Student中
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
对象变量没有进行初始化 (RandomAccessFile raf=new RandomAccessFile(*.txt,"rw");)
那为什么能用该类的方法 (Raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);)困饶.....会的人给我解答下。
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"zhangsan",98.5);
Student s2=new Student(2,"lisi",96.5);
Student s3=new Student(3,"wangwu",78.5);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);
for(long i=0;i<raf.length();i=raf.getFilePointer())
{
s.readStudent(raf);
System.out.println(s.num+":"+s.name+":"+s.score);
}
raf.close();
}
}class Student
{
int num;
String name;
double score;
public Student()
{
}
public Student(int num,String name,double score)
{
this.num=num;
this.name=name;
this.score=score;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
}
}
这个是一个简单的程序,没有什么大的问题,有个地方我不明白就是在自己定义的类Student中
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
对象变量没有进行初始化 (RandomAccessFile raf=new RandomAccessFile(*.txt,"rw");)
那为什么能用该类的方法 (Raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);)困饶.....会的人给我解答下。
解决方案 »
- 源代码看不出来!
- Native中在未传入jobject的方法中调用java方法
- 自己写的二分查找,如何跳出递归函数放回index
- javac?
- 问个简单的问题吧 关于javascript的<input type="submit" name="Submit" onclick="queryButtonClick()" value="提交"> 我怎样写queryB
- 问个问题:大家在用JSP做开发的时候,用到JSTL多吗?
- 关于java的容器布局问题.有jdk怎样布置介面?
- 求点拨一个Socket通信程序的调试.
- Class.newInstance()如何通过传入参数生成实例?
- 那位知道关于swing的电子书籍下载的地方,下谢了
- 谁有关于java package整体框架,java类框架图这方面的资料啊?(像MFC的组织类图的那种)
- 如何使JTable中的内容不能编辑?
new RandomAccessFile("student.txt","rw"); //这不是在构造方法中初始化了么?
这里传入的是一个对象,然后方法体中调用的是这个对象的方法。