根据返回的散列实现文件访问中的读和写对象,在读取和写入链时遇到一个错误EOFException。
我的写作方法是这样的。
private void escribirCadena( RandomAccessFile archivo, String cadena )throws IOException
{
StringBuffer bufer = null;
if ( cadena != null )bufer = new StringBuffer( cadena );
bufer.setLength( 6 );
archivo.writeChars( bufer.toString() );
}我登记的阅读方法是
public Persona obtenerRegistro( int i ) throws IOException{
if(i >= 0 && i < NumeroRegistros) {
archivo.seek(i * Persona.TAMANIO);
return new Persona(leerCadena(archivo),leerCadena(archivo));
//return leerCadena(archivo);
} else {
System.out.println("\nNúmero de registro fuera de límites.");
return null;
}
}
private String leerCadena( RandomAccessFile archivo ) throws IOException
{
char nombre[] = new char[6], temp;
for ( int cuenta = 0; cuenta < nombre.length; cuenta++ ) {
temp = archivo.readChar();
nombre[ cuenta ] = temp;
}
return new String( nombre ).replace( '\0', ' ' );
}我使用具有字符串类型的两个属性的附加数据对象,我为方法leerCadena标记错误,行temp = archivo.readChar();。
谢谢
发布于 2015-11-29 07:55:16
难道不是因为你的人数不够吗?
if(i >= 0 && i <= NumeroRegistros) {可能应该是:
if(i >= 0 && i < NumeroRegistros) {除此之外,几乎没有其他问题。就像StringBuffer被设置为10,但就在写之前,它被设置为30。
发布于 2015-11-29 07:45:46
我相信你读的文件超出了它的容量。你只应该读到文件(EOF)的末尾。检查用于读取文件的类的JavaDocs。它会告诉你“文件的终结”。
https://stackoverflow.com/questions/33980177
复制相似问题