1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Game implements Serializable{
private String nom, style;
private double prix;
public Game(String nom, String style, double prix) {
this.nom = nom;
this.style = style;
this.prix = prix;
}
public String toString(){
return "Nom du jeu : " + this.nom + "\n" +
"Style de jeu : " + this.style + "\n" +
"Prix du jeu : " + this.prix + "\n";
}
}
public void TCOSave3() {
//Nous déclarons nos objets en dehors du bloc try/catch
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(
new File(sketchPath+"game.txt"))));
//Nous allons écrire chaque objet Game dans le fichier
oos.writeObject(new Game("Assassin Creed", "Aventure", 45.69));
oos.writeObject(new Game("Tomb Raider", "Plateforme", 23.45));
oos.writeObject(new Game("Tetris", "Stratégie", 2.50));
//Ne pas oublier de fermer le flux !
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} |
Partager