| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 449 人关注过本帖
标题:向朋友们求教一个RandomAccessFile问题!
只看楼主 加入收藏
weibin0827
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-3-26
收藏
 问题点数:0 回复次数:4 
向朋友们求教一个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却不是很明白 希望懂的此题目的朋友 能帮助解答一下..谢谢了!
搜索更多相关主题的帖子: 朋友 
2007-03-26 21:26
angeloc
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:36
帖 子:1353
专家分:0
注 册:2006-11-21
收藏
得分:0 
有什么问题?不会用RandomAccessFile?而且为什么要用RandomAccessFile,题目要求么?

老牛明知夕阳晚,不用扬鞭自奋蹄; Angelo\'s BLOG
2007-03-26 21:49
weibin0827
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-3-26
收藏
得分:0 
对哈...题目要求的 而我却不知道怎么入手写这个程序 焦躁一晚上了 NND 
2007-03-26 21:55
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 

/**
* @(#)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;
}
}


我的msn: myfend@
2007-03-26 22:26
weibin0827
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-3-26
收藏
得分:0 
谢谢版主purana....我复制下来仔细看的..谢谢你能这么帮助我这个菜鸟..很感谢你!

2007-03-27 00:00
快速回复:向朋友们求教一个RandomAccessFile问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022488 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved