Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Lectura de archivo binario

He escrito los datos en un archivo binario. Pero no sé cómo leer desde un archivo binario. Para escribir tengo número de cuenta, nombre de usuario, contraseña, nombre para escribir en el archivo binario como se muestra a continuación:

 LinkedHashMap<String, String> details = new LinkedHashMap<String, String>(); details.put("Acount Number", "1986940573948"); details.put("Username", "Krischal"); details.put("Password", "8749578679"); details.put("Name","Krischal mahat"); String filename="dataFiles\\Customers.dat"; addCustomer (filename, details.values(), true); public static void addCustomer (String filename, Collection col, boolean append){ File file = new File (filename); ObjectOutputStream out = null; String str; try{ if (!file.exists () || !append) out = new ObjectOutputStream (new FileOutputStream (filename)); else out = new AppendableObjectOutputStream (new FileOutputStream (filename, append)); Iterator itr = col.iterator(); while (itr.hasNext()) { str = (String) itr.next(); out.writeObject(str); } out.writeObject("\n"); out.flush (); }catch (Exception e){ e.printStackTrace (); }finally{ try{ if (out != null) out.close (); }catch (Exception e){ e.printStackTrace (); } } }

Por favor, que alguien me ayude a leer los datos del cliente dando el número de cuenta de ese cliente del archivo binario. He usado el siguiente código para leer. Pero muestra errores. Solo quiero mostrar todos los datos del cliente proporcionando el número de cuenta.

 public static void check (String filename){ File file = new File (filename); if (file.exists ()){ ObjectInputStream ois = null; try{ ois = new ObjectInputStream (new FileInputStream (filename)); while (true){ String s = (String)ois.readObject (); System.out.println (s); } }catch (EOFException e){ }catch (Exception e){ e.printStackTrace (); }finally{ try{ if (ois != null) ois.close(); }catch (IOException e){ e.printStackTrace (); } } } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

'Binario' y 'línea por línea' son mutuamente contradictorios. Decídete. Está utilizando writeObject() , lo que significa binario y Serialización, y no debería escribir terminadores de línea en absoluto. Solo necesita usar ObjectInputStream.readObject() para leer este archivo.

Tampoco es necesario iterar. Simplemente escribe todo el objeto de la colección. O, de hecho, todo el Map , cada vez, sin anexar.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!