IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

Sérialisation des arrayList pour envoyé les fichiers via TCP


Sujet :

Entrée/Sortie Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2021
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 27
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2021
    Messages : 14
    Points : 9
    Points
    9
    Par défaut Sérialisation des arrayList pour envoyé les fichiers via TCP
    Bonjour,
    j'ai quelques difficultés avec la sérialisation, voila le topo : Dans le cadre d'un projet on doit créer une pointeuse d'un côté et une appli principale qui gère tous les pointages d'un autre côté. Les deux IHM s'envoient des données via le protocole TCP et le système de sauvegarde de tous les employé et leurs pointages se fait par sérialisation. J'ai donc essayé de créer une classe "Serialisation" qui permetterai d'envoyer les fichier serialiser d'une IHM à une autre via TCP. J'ai montré la classe que j'ai écrite à l'enseignant en charge du projet et m'a mis un stop. J'ai donc essayé de comprendre mieux comment tout cela fonctionnait mais je n'y arrive pas. Pourriez vous me dire ce qu'il ne va pas dans ma classe ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
     
    public class Serialisation implements Serializable{
     
    	private static ArrayList<Company> ListComp = new ArrayList<Company>();
    	private static ArrayList<Departement> ListDep = new ArrayList<Departement>();
    	private static ArrayList<Employee> ListEmp = new ArrayList<Employee>();
    	private static ArrayList<PlanningHebdo> ListPlanH = new ArrayList<PlanningHebdo>();
    	private static ArrayList<DayPlanning> ListDayPlan = new ArrayList<DayPlanning>();
    	private static ArrayList<Scoring> ListScoring = new ArrayList<Scoring>();
     
    	private File fichierCompany = new File("src/Company.ser");
    	private File fichierDepartement = new File("src/Departement.ser");
    	private File fichierEmployee = new File("src/Employee.ser");
    	private File fichierPlanningHebdo = new File("src/PlanningHebdo.ser");
    	private File fichierDayPlanning = new File("src/DayPlanning.ser");
    	private File fichierScoring = new File("src/Scoring.ser");
     
     
     
    	public void SerializeCompany(Company comp) {
    		ListComp.add(comp);
    		try {
    			ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(fichierCompany)) ;
    			oos.writeObject(ListComp);
    			oos.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void SerializeDepartement(Departement dep) {
    		ListDep.add(dep);
    		try {
    			ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(fichierDepartement)) ;
    			oos.writeObject(ListDep);
    			oos.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void SerializeEmployee(Employee emp) {
    		ListEmp.add(emp);
    		try {
    			ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(fichierEmployee)) ;
    			oos.writeObject(ListEmp);
    			oos.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void SerializePlanningH(PlanningHebdo PlanH) {
    		ListPlanH.add(PlanH);
    		try {
    			ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(fichierPlanningHebdo)) ;
    			oos.writeObject(ListPlanH);
    			oos.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void SerializeDayPlanning(DayPlanning dayP) {
    		ListDayPlan.add(dayP);
    		try {
    			ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(fichierDayPlanning)) ;
    			oos.writeObject(ListDayPlan);
    			oos.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void SerializeScoring(Scoring scor) {
    		ListScoring.add(scor);
    		try {
    			ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(fichierScoring)) ;
    			oos.writeObject(ListScoring);
    			oos.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public ArrayList<Company> DeserializeCompany() {
    		try {
     
    			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(fichierCompany)) ;
    			ListComp =  (ArrayList<Company>) ois.readObject();
    			ois.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    		catch(ClassNotFoundException ex)
    		{
    			ex.printStackTrace();
    		}
    		return ListComp;
    	}
     
    	public ArrayList<Departement> DeserializeDepartement() {
    		try {
    			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(fichierDepartement)) ;
    			ListDep = (ArrayList<Departement>) ois.readObject();
    			ois.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    		catch(ClassNotFoundException ex)
    		{
    			ex.printStackTrace();
    		}
    		return ListDep;
    	}
     
    	public ArrayList<Employee> DeserializeEmployee() {
    		try {
    			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(fichierEmployee)) ;
    			ListEmp = (ArrayList<Employee>) ois.readObject();
    			ois.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    		catch(ClassNotFoundException ex)
    		{
    			ex.printStackTrace();
    		}
    		return ListEmp;
    	}
     
    	public ArrayList<PlanningHebdo> DeserializePlanningHebdo() {
    		try {
    			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(fichierPlanningHebdo)) ;
    			ListPlanH = (ArrayList<PlanningHebdo>) ois.readObject();
    			ois.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    		catch(ClassNotFoundException ex)
    		{
    			ex.printStackTrace();
    		}
    		return ListPlanH;
    	}
     
    	public ArrayList<DayPlanning> DeserializeDayPlanning() {
    		try {
    			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(fichierDayPlanning)) ;
    			ListDayPlan = (ArrayList<DayPlanning>) ois.readObject();
    			ois.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    		catch(ClassNotFoundException ex)
    		{
    			ex.printStackTrace();
    		}
    		return ListDayPlan;
    	}
     
    	public ArrayList<Scoring> DeserializeScoring() {
    		try {
    			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(fichierScoring)) ;
    			ListScoring = (ArrayList<Scoring>) ois.readObject();
    			ois.close();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    		catch(ClassNotFoundException ex)
    		{
    			ex.printStackTrace();
    		}
    		return ListScoring;
    	}
     
     
    }

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Hello,

    comme gros problèmes, je vois :

    - cette classe implémente Serializable, alors que ce ne sont jamais ses instances à elle, qui sont sérialisées. Ce sont les autres classes, comme Company, Department, etc, qui doivent implémenter Serializable.

    - Usage incohérent de static : toutes tes variables List sont statiques, mais les méthodes qui s'en servent ne le sont pas. Ça ne va pas, ça. Il faut choisir : soit la classe est utilisée de manière statique, soit elle est instanciée. C'est l'un ou c'est l'autre, on mélange pas. (A la rigueur, une classe peut avoir des parties statiques et des parties instanciées. Mais toi tes variables statiques sont utilisées par des méthodes d'instance, c'est n'importe quoi.)

    - Quel est l'intérêt de faire des catch sur les exceptions, si tout ce que tu en fais c'est un printStackTrace() ? Afficher une erreur dans une console ne résout pas cette erreur. Si la méthode n'a pas fait son travail, elle doit lever une exception. Il ne faut donc pas de catch, il faut laisser l'exception remonter.

    Par ailleurs, je ne vois pas trop le rapport entre des fichiers, et de la communication TCP. Bon, les fichiers, ça a l'avantage d'être plus persistant que la mémoire, un fichier sera toujours là même si on arrête la machine. Du coup je ne dis pas que c'est mal. Mais, ça n'a rien à voir avec de la communication réseau, ça.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2021
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 27
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2021
    Messages : 14
    Points : 9
    Points
    9
    Par défaut
    Merci pour ta réponse je vais refaire en prenant en compte ce que tu m'as dit
    Et pour ce qu'y est la partie réseaux je voulais que les fichiers créer s'envoient d'une IHM à une autre avec des sockets. Bon .. je commence avec tous ca c'est sans doute pas très pertinant ^^ Mais merci beaucoup en tout cas !

  4. #4
    Membre habitué
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Janvier 2008
    Messages
    251
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Janvier 2008
    Messages : 251
    Points : 192
    Points
    192
    Par défaut
    Il y a des années j'ai écrit ces méthodes de sérialisation et désérialisation entre objet Java et byte[]...je pense que cela pourrait être utile.

    Le byte[] tu peux faire ce que tu veux avec...fichier...TCP...ce ne sont que des tableaux d'octets.

    https://www.developpez.net/forums/d1...se-postgresql/

Discussions similaires

  1. Réponses: 20
    Dernier message: 18/10/2016, 18h02
  2. script pour envoyer un ordre via TCP de Windows vers Ubuntu
    Par manganat dans le forum Scripts/Batch
    Réponses: 3
    Dernier message: 09/02/2016, 17h48
  3. Créer des noms courts pour tout les fichiers
    Par Antonin21 dans le forum Windows XP
    Réponses: 0
    Dernier message: 19/02/2012, 18h02
  4. Réponses: 6
    Dernier message: 05/12/2008, 21h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo