为什么我会收到java.io.EOFException和ArrayOutOfBounds错误?救命?

时间:2011-12-05 05:51:33

标签: java arrays file eof eofexception

我正在尝试创建一种简单的GUI,我试图保存一些字符串和双精度和一个int,我使用OOP的基本属性继承我创建了一个类Autos本质上是超类。

问题似乎出现在名为“cargarDatosAutos”的方法中,这是在我的GUI类中,这里是代码:

private void cargarDatosAutos()
{
    regInt = at.numRegistros(); // number of registry
    if (regInt != -1)
    {
        curInt = 0;
        ats = new AutosRentables[regInt];
        try
        {
            RandomAccessFile f = new RandomAccessFile("Autos.txt", "rw");
            at.cargarDatos(f, ats, regInt); // method in subclass
            f.close();
        }
        catch (IOException ex)
        {
            Logger.getLogger(Interfaz3.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.mostrarAutos(ats[0]); // shows data
    }
}

以下是错误:

4-Dec-2011 11:35:20 PM rent_autos.Interfaz3 cargarDatosAutos
SEVERE: null
java.io.EOFException
at java.io.RandomAccessFile.readChar(RandomAccessFile.java:695)
at rent_autos.Autos.leerModelo(Autos.java:139)
at rent_autos.AutosRentables.cargarDatos(AutosRentables.java:84)
at rent_autos.Interfaz3.cargarDatosAutos(Interfaz3.java:6076)
at rent_autos.Interfaz3.<init>(Interfaz3.java:38)
at rent_autos.Interfaz3$159.run(Interfaz3.java:6107)

leerModelo是一个读取字符串的方法:

public String leerModelo(RandomAccessFile file) throws IOException
{
    char cadena[] = new char[25], temp;
    for (int c = 0; c < cadena.length; c++)
    {
        temp = file.readChar();
        cadena[c] = temp;
    }
    return new String(cadena).replace('\0', ' ');
}

cargarDatos将加载我的数据:

public void cargarDatos(RandomAccessFile file, AutosRentables[] lista, int reg) throws IOException
{
    int cont = 0;
    do
    {

        modelo = this.leerModelo(file);
        color = this.leerColor(file);
        tipoAM = this.leerTipoAM(file);
        rendimientoGalon = file.readDouble();
        placa = this.leerPlaca(file);
        ACRISS = this.leerACRISS(file);
        codigo = file.readInt();
        costo = file.readDouble();
        marca = this.leerMarca(file);
        detalles = this.leerDetalles(file);

        lista[cont] = new AutosRentables(modelo, color, tipoAM, rendimientoGalon, placa, ACRISS, codigo, costo, marca, detalles);
        cont++;
        System.out.println("Entra");
    }
    while (cont < reg);
}

然后是ArrayoutOfbound错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at rent_autos.Interfaz3.cargarDatosAutos(Interfaz3.java:6081)
at rent_autos.Interfaz3.<init>(Interfaz3.java:38)
at rent_autos.Interfaz3$159.run(Interfaz3.java:6107)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

所以,如果有人知道最新情况,请帮助我...这是文件的字节大小吗?我真的不知道,帮助!

2 个答案:

答案 0 :(得分:1)

EOFException表示您尝试阅读流的末尾;即在这种情况下文件的结尾。可能你没有在文件中正确定位自己。从随机访问文件中读取字符很棘手,因为您无法知道它们被编码为多少字节。我怀疑你需要重新设计文件。否则,如果它来自外部系统,你应该读取字节而不是字符?

答案 1 :(得分:1)

java.io.EOFException的:

对于java.io.EOFException,请检查此链接http://docs.oracle.com/javase/1.4.2/docs/api/java/io/EOFException.html

ArrayOutOfBounds: 当您尝试访问索引超过其长度的数组时,会发生超出范围的异常。 java数组的最大索引是(长度-1)。这意味着您正在尝试将值插入到数组元素中而不存在。 为了处理它,你应该确保你的程序不能访问索引大于length的数组 -