【发布时间】: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