在你認爲這只是很長時間之前,請儘量幫我解決這個問題整天(9:52 AM - 4 :54 PM),它只是在煩擾我。爲什麼當我試圖保存數據在TXT(JAVA)時,我得到這些符號
我似乎無法正確保存這些數據我主要是雙打和字符串,我在一個名爲「autosAudi.txt」 .txt文件保存這些,我得到這些奇怪的碼和包裝盒,有時在無論什麼原因中國我的文本字段?這是什麼出現一個例子:
http://www.flickr.com/photos/[email protected]/6449175293
,你可以看到這些奇怪的箱子出現了無處我沒有註冊不一樣的是,我通過數據瀏覽方式是通過點擊>>(下一個)或< < <(previous),並且信息應該改變,「Numero」旁邊的文本字段應該給我一種輸入int代碼的方式,以便我可以搜索數據(例如,如果我寫1,它應該找到該註冊表)。它僅適用於1號之後,它給出了這樣的錯誤:
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)
這裏是我用於保存在「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();
}
}
而且還使用:
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;
}
的escribirCadena方法在字符串類型一起使用,因爲Java不必須.writeString,繼承人對於該代碼:
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());
}
而且leerCadena方法是閱讀字符串:
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',' ');
}
我做了基於我的導師的例子本項目和文件確實是不同的,繼承人的圖片:
http://www.flickr.com/photos/[email protected]/6449172477/in/photostream/
說「Registro.txt」是我的老師和「autosAudi.txt」是我的
繼承人最後一件事:搜索方法那種長:
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();
}
因此,誰能幫助我在這裏!這將是真正偉大的,我知道它的那種長,但如果任何人有解決這個請分享知識,是的,我haved嘗試.dat文件類型
你爲什麼不首先從今天回覆您的活動和類似先前的問題發佈呢?問題已經得到解答。如果答案讓你感到困惑,那就在你之前的問題中發表評論,並澄清你之前的問題。 -1。 –