| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 613 人关注过本帖
标题:请教各位
取消只看楼主 加入收藏
java在线
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2005-7-9
结帖率:100%
收藏
 问题点数:0 回复次数:0 
请教各位
public class Student {
    private String name;
    private int score;
    public Student()
    {
        setName("noname");
    }
    public Student(String name,int score)
    {
        setName(name);
        this.score=score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        StringBuilder builder=null;
        if(name!=null)
        builder=new StringBuilder(name);
        else
            builder=new StringBuilder(15);
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public static int size()
    {
        return 34;
    }
}
import
import
import
import java.util.Scanner;

public class RandomAccessFileDemo {

    public static void main(String[] args) {
        Student[] students = { new Student("zhangsheng", 90),
                new Student("liyuan", 92), new Student("goofy", 90) };
        try {
            File file = new File(args[0]);
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

            for (int i = 0; i < students.length; i++) {
                randomAccessFile.writeChars(students[i].getName());
                randomAccessFile.writeInt(students[i].getScore());
            }
            Scanner scanner = new Scanner(System.in);
            System.out.print("读取第几个数据");
            int num = scanner.nextInt();
            randomAccessFile.seek((num-1) * Student.size());
            Student student = new Student();
            student.setName(readName(randomAccessFile));
            student.setScore(randomAccessFile.readInt());
            System.out.println("姓名:" + student.getName());
            System.out.println("分数:" + student.getScore());
            randomAccessFile.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("请指定文件名称");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static String readName(RandomAccessFile randomAccessFile)
            throws IOException {
        char[] name = new char[15];
        for (int i = 0; i < name.length; i++)
            name[i] = randomAccessFile.readChar();
        return new String(name).replace('\0', ' ');
    }

}
良哥哥的书上的例子,运行怎么就抛出ArrayIndexOutOfBoundsException异常啊?在Eclipse上运行的
搜索更多相关主题的帖子: private builder public return null 
2008-04-25 03:21
快速回复:请教各位
数据加载中...
 
   



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

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