I cant seem to save these data correctly I have mainly doubles and strings, I am saving these in a .txt file called "autosAudi.txt", I get these weird codes and boxes and sometimes for whatever reason Chinese in my textfields? Here is a example of what appears:
http://www.flickr.com/photos/63259070@N06/6449175293
as you can see these weird boxes appear out nowhere I didnt register nothing like that, the way I navigate through the data is by clicking >> (next) or << (previous) and the information is supposed to change, the textfield next to "Numero" is supposed to give me a way to input an int code so I can search the data(for example if I write 1, its supposed to find that registry). it only works for the number 1 and after that it gives this error:
run:
3-Dec-2011 5:02:01 PM rent_autos.Interfaz3 jbBucar_AMouseClicked
SEVERE: null
java.io.EOFException
at java.io.RandomAccessFile.readChar(RandomAccessFile.java:695)
at rent_autos.Autos.leerCadena(Autos.java:276)
at rent_autos.Autos.buscar(Autos.java:344)
at rent_autos.Interfaz3.jbBucar_AMouseClicked(Interfaz3.java:4640)
at rent_autos.Interfaz3.access$1100(Interfaz3.java:30)
at rent_autos.Interfaz3$12.mouseClicked(Interfaz3.java:745)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Here is the code that I used for saving the file in the "autosAudi.txt":
public void guardar() throws IOException {
int opcion = JOptionPane.showConfirmDialog(null,"¿Desea guardar los
cambios?","Datos Persona",JOptionPane.YES_NO_CANCEL_OPTION);
if(opcion == 0){
RandomAccessFile f = new RandomAccessFile("autosAudi.txt","rw");
f.seek(f.length());
f.writeInt(codigo);
f.writeDouble(costo);
f.writeDouble(rendimientoGalon);
escribirCadena(f, ACRISS);
escribirCadena(f, color);
escribirCadena(f, modelo);
escribirCadena(f, tipoAM);
escribirCadena(f, placa);
escribirCadena(f, marca );
f.close();
}
}
And also used:
public boolean cargarDatosRegistro(long iPosicion, String sRuta){
boolean bRealizado = true;
try {
RandomAccessFile fRegistro = new RandomAccessFile(sRuta,"rw");
fRegistro.seek(iPosicion);
codigo = fRegistro.readInt();
costo = fRegistro.readDouble();
rendimientoGalon = fRegistro.readDouble();
ACRISS = leerCadena(fRegistro);
color = leerCadena(fRegistro);
modelo = leerCadena(fRegistro);
tipoAM = leerCadena(fRegistro);
placa = leerCadena(fRegistro);
marca = leerCadena(fRegistro);
fRegistro.close();
} catch (Exception e) {
bRealizado = false;
}
return bRealizado;
}
The escribirCadena method is used on String types, because java doesnt have .writeString, heres the code for that:
private void escribirCadena( RandomAccessFile file, String cadena) throws IOException
{
StringBuffer buffer = (cadena == null)? new StringBuffer(15):
new StringBuffer(cadena);
buffer.setLength(30);
file.writeChars(buffer.toString());
}
And the leerCadena Method is for reading Strings:
private String leerCadena( RandomAccessFile file ) throws IOException {
char cadena[] = new char[30], temp;
for ( int c=0;c<cadena.length;c++)
{
temp = file.readChar();
cadena[c]=temp;
}
return new String(cadena).replace('\0',' ');
}
I made this project based on the example of my tutor and the files are really different, heres the pic:
http://www.flickr.com/photos/63259070@N06/6449172477/in/photostream/
The one that says "Registro.txt" is my teachers and the "autosAudi.txt" is mine
Heres one last thing: the search method kind of long:
public void buscar(int cod) throws IOException{
RandomAccessFile f = new RandomAccessFile("autosAudi.txt","rw");
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
costo = f.readDouble();
rendimientoGalon = f.readDouble();
ACRISS = leerCadena(f);
color = leerCadena(f);
modelo = leerCadena(f);
tipoAM = leerCadena(f);
placa = leerCadena(f);
marca = leerCadena(f);
if(cod==codigo){
iCodigoBusqueda = codigo;
encontrado=true;
registroExistente = true;
break;
}else{
iCodigoBusqueda = 0;
registroExistente = false;
}
bytes += iTamanioRegistro;
f.seek(bytes);
}
while(bytes<f.length());
if(!encontrado){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Archivo no encontrado");
}
f.close();
}
So can anyone help me out here!, it would be really great I know its kind of long but if anyone has the knowledge to solve this please share, and yes I haved tries .dat file types