Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read/write object vector to/from file

Status
Not open for further replies.

p27br

Programmer
Aug 13, 2001
516
GB
hi,

i am new to java and have coded some functions that should write and read vectors of objects to a file.
It seems the write routine is working because there is some data in the file, but the read routine doesn't manage to load back the vector

here is the code for the file handling class :
Code:
package untitled10;
import java.io.*;
public class Fichier extends File {
   public Fichier(String nomFichier)  {
     super(nomFichier);
   }
   public boolean lirePossible() {
     return (this.exists()&&this.isFile()
             &&this.canRead());
   }
   public boolean ecrirePossible() {
     boolean fichierEnregistrable;
     if (this.exists())
       if (this.isFile() && this.canWrite())
         fichierEnregistrable=true;
       else
         fichierEnregistrable=false;
     else {
       String nomAbsolu=this.getAbsolutePath();
       String nomRepertoire=
        nomAbsolu.substring
             (0,nomAbsolu.lastIndexOf(File.separator));
       File repertoire=new File(nomRepertoire);
       if (repertoire.exists() && repertoire.canWrite())
         fichierEnregistrable=true;
       else
         fichierEnregistrable=false;
     }
     return fichierEnregistrable;
   }
   public String proprietes() {
     StringBuffer s=new StringBuffer();
     if (!this.exists())
       s.append("fichier non existant");
     else if (this.isFile()) {
       s.append("fichier");
       if (this.canRead())
         s.append(" acces en lecture");
       if (this.canWrite())
         s.append(" acces en ecriture");
       }
     else if (this.isDirectory())
       s.append("repertoire");
     return new String(s);
   }
   public String parent() {
     String nomAbsolu=this.getAbsolutePath();
     return nomAbsolu.substring(0,
        nomAbsolu.lastIndexOf(File.separator));
   }
}

the write routine
Code:
    public static void writeVectorToFile(Fichier f, Vector v) {
        if (f.ecrirePossible()) {
            try {
                ObjectOutputStream flotEcriture =
                        new ObjectOutputStream(
                                new FileOutputStream(f));

                flotEcriture.writeObject(v);
                flotEcriture.close();
            } catch (IOException e) {
                System.out.println(" erreur :" + e.toString());
            }
        } else {
            System.out.println("Erreur ecriture" + " "
                               + f.proprietes());
        }
    }

the read routine
Code:
        public static Vector loadVectorFromFile(Fichier f) {
        Vector v = new Vector();
        //Object lu = new Object();
        if (!f.lirePossible()) {
            System.out.println("bob"
                               + " " + f.proprietes());
            System.exit(0);
        }
        try {
            ObjectInputStream flotLecture =
                    new ObjectInputStream(
                            new FileInputStream(f));
            v = (Vector)flotLecture.readObject();
            //v = (Vector)lu;
            flotLecture.close();
        }

        catch (Exception e) {
            System.out.println(" erreur :" + e.toString());
        }

        return v;
    }

and the calls :

Code:
    vUtilisateurs = loadVectorFromFile(fUtil);
    //run once - default admin
    //ajoutUtilisateur("doe","john","pforpf","sesame",
     //"emailaddress");
      //if (!vUtilisateurs.isEmpty())
     // writeVectorToFile(fUtil,vUtilisateurs);

any help much appreciated
 
i should add the Utilisateur (User) class I'm trying to read/write

Code:
package untitled10;
import java.io.*;
import java.util.*;
public class Utilisateur implements Serializable{

    private String nomUtilisateur;
    private String prenomUtilisateur;
    private String identifiantUtilisateur;
    private String motDePasse;
    //AdresseEmail emailUtilisateur;
    private Vector historiqueCommande;
    double budget;
    boolean estActif;
    boolean estLoggable;
    public Utilisateur(String nom,String prenom,String identifiant,
                       String motdepasse,String adr) {
        AdresseEmail emailUtilisateur = new AdresseEmail(adr);
    }

and the email address

Code:
package untitled10;
import java.io.*;

public class AdresseEmail implements Serializable{
    private String nom;
            String domaine;
            boolean estValide;
    public AdresseEmail(String adr) {
        nom = adr;
        //nom = adr.left
        //domaine = adr.right
        // estValide = test
    }
}
 
i found the problem : there was a logic error in a test
Code:
if (u != null)

changed to
Code:
if (u == null)

thanks anyway if you took the time to read
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top