【问题标题】:Why do I get the java.io.EOFException and the ArrayOutOfBounds error? Help?为什么会出现 java.io.EOFException 和 ArrayOutOfBounds 错误?帮助?
【发布时间】:2011-12-05 05:51:32
【问题描述】:

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

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

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)

所以如果有人知道发生了什么事,请在这里帮我...是文件的字节大小吗?,我真的不知道,帮助!

【问题讨论】:

  • 与您的帖子无关,但为什么您在代码中使用非英语语言?我从事过这样一个项目,这是有史以来最糟糕的事情。
  • @cherouvim 可能是因为 OP 的母语不是英语。
  • @DaveNewton 我也不是,但我总是用英语编码,因为我不希望你继承我的希腊语应用程序代码;)
  • 我假设您的代码中的 leerModelo(file) 是以随机访问模式从文件中每 25 个字符读取一次模型数据。问题是文件可能在那一刻仍然是空的,或者没有足够的字符(长度少于 25 个字符)并且您没有进行检查以避免此类陷阱。如果不验证读取的数据,在随机访问模式下读取是非常危险的

标签: java arrays file eof eofexception


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    java.io.EOFException:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多