Ecrire et Relire un Obj dans un fichier - Java - Programmation
Marsh Posté le 06-05-2004 à 18:25:19
Je voudrais écrire puis relire un objet dans un fichier. /*LA CLASS FICHIER*/ public class Fichier { private FileOutputStream fos; private ObjectOutputStream oos; private FileInputStream fis; private ObjectInputStream ois; private String fileName; public Fichier(String fileName) throws IOException { this.fileName = fileName; fos = new FileOutputStream(this.fileName); oos = new ObjectOutputStream(fos); fis = new FileInputStream(this.fileName); ois = new ObjectInputStream(fis); } public void write(Object o) throws IOException { oos.writeObject(o); } public Object read() throws IOException, ClassNotFoundException, EOFException { return ois.readObject(); } public void close() throws IOException { if(oos != null)oos.close(); if(ois != null)ois.close(); } } /*UTILISATION*/ private void read() throws EOFException, IOException, ClassNotFoundException { try { Msn.myFile = new Fichier(fileName); //Msn.myFile.write(new ListeUser()); listeUser = (ListeUser)Msn.myFile.read(); Msn.myFile.close(); } catch (Exception e) { System.out.println(e); System.out.println("new file" ); listeUser = new ListeUser(); Msn.myFile.write(listeUser); System.out.println(Msn.myFile.read()); Msn.myFile.close(); } Le hic, c'est que a chaque ouvertue de prog, il m'ecrit nouveau fichier Merci de votre aide. Ponpon
Make sure you enter the(*)required information where indicate.HTML code is not allowed
Marsh Posté le 06-05-2004 à 18:25:19
Je voudrais écrire puis relire un objet dans un fichier.
/*LA CLASS FICHIER*/
public class Fichier
{
private FileOutputStream fos;
private ObjectOutputStream oos;
private FileInputStream fis;
private ObjectInputStream ois;
private String fileName;
public Fichier(String fileName) throws IOException
{
this.fileName = fileName;
fos = new FileOutputStream(this.fileName);
oos = new ObjectOutputStream(fos);
fis = new FileInputStream(this.fileName);
ois = new ObjectInputStream(fis);
}
public void write(Object o) throws IOException
{
oos.writeObject(o);
}
public Object read() throws IOException, ClassNotFoundException, EOFException
{
return ois.readObject();
}
public void close() throws IOException
{
if(oos != null)oos.close();
if(ois != null)ois.close();
}
}
/*UTILISATION*/
private void read() throws EOFException, IOException, ClassNotFoundException
{
try
{
Msn.myFile = new Fichier(fileName);
//Msn.myFile.write(new ListeUser());
listeUser = (ListeUser)Msn.myFile.read();
Msn.myFile.close();
}
catch (Exception e)
{
System.out.println(e);
System.out.println("new file" );
listeUser = new ListeUser();
Msn.myFile.write(listeUser);
System.out.println(Msn.myFile.read());
Msn.myFile.close();
}
Le hic, c'est que a chaque ouvertue de prog, il m'ecrit nouveau fichier
Merci de votre aide.
Ponpon