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

I THINK that is a problem with the character encoding. I don't know how to fix it, but I thought that that might be a future Google search.

Sorry about my un-helpfulness.

-Jack

Your bits and pieces of program code are hard to piece together to make a complete program.
Can you write a small simple program that compiles and executes and shows your problem?
The small squares you see look like you are reading binary data (like int or double) and treating it like Strings.

Member Avatar for hfx642

I don't think that it is a problem with your code.
(Although, I have really looked at your code.)
I think it's more of a problem with your computer.
(ie. The fonts that your computer is using.)

You seem to be using methods like writeInt, writeDouble on a RandomAccessFile. These methods write the data in its internal binary format, so you would expect to see garbage if you try to view those bytes as text.

Member Avatar for hfx642

I was just looking at your screen shot, and not at your code.
(I tend to ignore code if there is too much to read.)
I think that JamesCherrill has the correct answer to your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.