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
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    Janvier 2021
    Messages
    14
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 29
    Localisation : France, Indre et Loire (Centre)

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

    Informations forums :
    Inscription : Janvier 2021
    Messages : 14
    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 582
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 582
    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
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    Janvier 2021
    Messages
    14
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 29
    Localisation : France, Indre et Loire (Centre)

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

    Informations forums :
    Inscription : Janvier 2021
    Messages : 14
    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 �clair�
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Janvier 2008
    Messages
    254
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 55
    Localisation : Belgique

    Informations professionnelles :
    Activit� : Consultant informatique

    Informations forums :
    Inscription : Janvier 2008
    Messages : 254
    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