当前位置: 代码迷 >> Java相关 >> 向朋友们求教一个RandomAccessFile问题!
  详细解决方案

向朋友们求教一个RandomAccessFile问题!

热度:171   发布时间:2007-03-26 21:26:47.0
向朋友们求教一个RandomAccessFile问题!
题目是如下:

Write an application that asks a user to enter student data and writes a random data file using student's ID number as a key field. Assuming a student record contains only an ID number(int type) and grade point average(double type). Each student Id number is three digits or few. Use JoptionPane for the input. Save the program as WriteStudentFile.java.


这是一道练习题...可我对RandomAccessFile却不是很明白 希望懂的此题目的朋友 能帮助解答一下..谢谢了!
搜索更多相关的解决方案: 朋友  

----------------解决方案--------------------------------------------------------
有什么问题?不会用RandomAccessFile?而且为什么要用RandomAccessFile,题目要求么?
----------------解决方案--------------------------------------------------------
对哈...题目要求的 而我却不知道怎么入手写这个程序 焦躁一晚上了 NND 
----------------解决方案--------------------------------------------------------

/**
* @(#)RandomAccessFileTest.java
*
*
* @author if
* @version 1.00 2007/3/26
*/

import java.io.RandomAccessFile;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;

public class RandomAccessFileTest {
public static void main(String[] args) throws Exception{
int nStu=Integer.parseInt(JOptionPane.showInputDialog(null,"你有多少个学生?"));
Student stu=null;
RandomAccessFileWriteRead ranWriteReadFile=new RandomAccessFileWriteRead("stu.txt");

for(int i=0;i<nStu;i++){
int id=Integer.parseInt(JOptionPane.showInputDialog(null,"第"+(i+1)+"个学生的ID是"));
double grade=Double.parseDouble(JOptionPane.showInputDialog(null,"第"+(i+1)+"个学生的Grade是"));
stu=new Student();
stu.setID(id);
stu.setGrade(grade);
ranWriteReadFile.writeRandomFile(stu);
}

while(true){
int id=Integer.parseInt(JOptionPane.showInputDialog(null,"你要查看第几个学生的成绩?输入-1退出!"));
if(id==-1) break;
String result=ranWriteReadFile.readRandomFile(id);
System.out.println(result);
}
}
}

class RandomAccessFileWriteRead {
private RandomAccessFile randomFile=null;
private File file=null;

public RandomAccessFileWriteRead(String filename){
file=new File(filename);
}

public void writeRandomFile(Student stu) throws IOException{
randomFile=new RandomAccessFile(file,"rw");
long point=randomFile.length();
randomFile.seek(point);
int id=stu.getID();
double grade=stu.getGrade();
randomFile.writeInt(id);
randomFile.writeDouble(grade);
randomFile.close();
}

public String readRandomFile(int n) throws IOException{
randomFile=new RandomAccessFile(file,"r");
randomFile.seek((n-1)*12);
Student stu=new Student();
stu.setID(randomFile.readInt());
stu.setGrade(randomFile.readDouble());
randomFile.close();
return stu.toString();
}
}

class Student {
private int id;
private double grade;

public void setID(int id){
this.id=id;
}

public int getID(){
return this.id;
}

public void setGrade(double grade){
this.grade=grade;
}

public double getGrade(){
return this.grade;
}

public String toString(){
return "ID: "+id+" Grade: "+grade;
}
}


----------------解决方案--------------------------------------------------------
谢谢版主purana....我复制下来仔细看的..谢谢你能这么帮助我这个菜鸟..很感谢你!


----------------解决方案--------------------------------------------------------