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 :
the write routine
the read routine
and the calls :
any help much appreciated
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