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érialiser/désérialiser une Hashtable : pb de cast(?)


Sujet :

Entrée/Sortie Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 4
    Points : 2
    Points
    2
    Par défaut sérialiser/désérialiser une Hashtable : pb de cast(?)
    Bonjour à tous,

    Je voudrais sérialiser puis désérialiser une Hashtable mais je bataille avec un "unchecked or unsafe operations" .
    J'ai lu bcp de tutos et de messages (génériques, sérialisation, cast )mais je ne vois tjs pas : Hashtable paramétrée, Serializable implantée partout...

    voici mon code:
    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
     
    ...
     Compte testCompte= new CompteCourant("DUSCHMOLL", "Gérard");
     FileOutputStream fos=null;
     ObjectOutputStream myOut=null;
     
     try{     fos= new FileOutputStream("test2.dat");
                myOut= new ObjectOutputStream(fos);
                myOut.writeObject(testCompte);
                myOut.flush();
     }
     catch(FileNotFoundException fnfe){System.out.println("Le fichier mentionné  n'a pu être trouvé !");}  
     catch(IOException e1){System.out.println("Une erreur d'entrée sortie s'est produite !!");}
     
     finally{ if(myOut!=null){myOut.close();}
    }   
     
     
     FileInputStream fis=null;
      ObjectInputStream myIn=null;
         try{
                   fis=new FileInputStream("test2.dat");
                   myIn= new ObjectInputStream(fis);
                   Hashtable<String, Compte> sortie= new Hashtable<String, Compte>();
                   //c'est ce cast-là qui est refusé :
                   sortie = (Hashtable<String, Compte>)myIn.readObject();
                   System.out.println(sortie.keySet()); 
       }
       catch(FileNotFoundException fnfe){System.out.println("Le fichier mentionné n'a pu être trouvé !");}  
       catch(ClassNotFoundException cnfe){System.out.println("classe non  trouvée !!");}
       catch(IOException e1){System.out.println("Une erreur d'entrée sortie s'est produite !!");}
     
     finally{ if(myIn!=null){myIn.close();}
    }
    pour info, la classe abstraite "Compte" a entre autre comme fille la classe "CompteCourant".

    Tous mes remerciements à ceux qui pourront me donner qq indices !

  2. #2
    Membre expert

    Homme Profil pro
    Consultant informatique
    Inscrit en
    Janvier 2004
    Messages
    2 301
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2004
    Messages : 2 301
    Points : 3 675
    Points
    3 675
    Par défaut
    Hummmm... Tu sauve un "Compte" et tu essayes de recharger une "HashTable"?

    pas étonnant que ça coince
    "Le plug gros problème des citations trouvées sur internet, c'est qu'on ne peut jamais garantir leur authenticité"

    Confucius, 448 av. J-C

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Bonjour Pill_s,

    ooupps un ptérodactyle a dû se poser sur mes lunettes qd j'ai copié mon
    code
    Cependant, je pense avoir déjà testé avec la hashtable en paramètre du writeObject().
    Je reviens dès que j'ai un code sans cette bourde monstrueuse !

    Merci de la réponse rapide :-)

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    re-bonjour,

    J'ai trouvé une solution je crois

    Tout d'abord voici le code sans la bourde (mais avec le warning !):
    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
     
       //CREATION DU HASHTABLE
        public static Hashtable<String,Agence> listeAgences= new    Hashtable<String, Agence>();
        public static String fich="test2.dat";
     
        public static void main(String[] args)throws IOException{
     
            //AJOUT DE 4 AGENCES DS LE HASHTABLE
            Agence agence1 = new Agence("Marseille", "Mairie", "agence_marseille.dat"); 
            Agence agence2 = new Agence("Saint-Ouen", "Carnot", "agence_stouen.dat");
            Agence agence3 = new Agence("Lille", "Centre", "agence_lille.dat");
            Agence agence4 = new Agence("Nice", "Plage", "agence_nice.dat");
            String s1=agence1.getCode();
            String s2=agence2.getCode();
            String s3=agence3.getCode();
            String s4=agence4.getCode();
            listeAgences.put(s1, agence1);
            listeAgences.put(s2, agence2);
            listeAgences.put(s3, agence3);
            listeAgences.put(s4, agence4);
     
     
        //SERIALISATION
        //public static void serialiser(Hashtable<String, ?> hash, String fichier) throws IOException{
            FileOutputStream fos=null;
            ObjectOutputStream myOut=null;
            try{
                fos= new FileOutputStream(fich);
                myOut= new ObjectOutputStream(fos);
                myOut.writeObject(listeAgences);
                myOut.flush();
            }
            catch(FileNotFoundException fnfe){System.out.println("Le fichier mentionné n'a pu être trouvé !");
            }  
            catch(IOException e1){System.out.println("Une erreur d'entrée sortie s'est produite !!");
            }
            finally{ if(myOut!=null){ myOut.close();}
            }  
     
            System.out.println("Opération terminée (?)");
       // } 
         //DESERIALISATION 
     
           FileInputStream fis=null;
           ObjectInputStream myIn=null;
           try{
               fis=new FileInputStream(fich);
               myIn= new ObjectInputStream(fis);
               //PB DE CAST ICI : WARNING
               Hashtable<String,Agence> sortie = (Hashtable<String, Agence>)myIn.readObject();
               //énumération pour affichage; 
               Enumeration en = sortie.keys();
               while(en.hasMoreElements()){
                       Object o= en.nextElement();
                       System.out.println( (sortie.get(o)).toString() );
               }
    Vu qu'un petit .getClass() sur myIn.readObject() donne "java.util.Hashtable",
    je pense que le compilateur ne reconnaît pas le paramètrage du hashtable.

    Si on utilise les wildcards, le warning disparaît :
    Hashtable<?,?> sortie = (Hashtable<?, ?>)myIn.readObject();

    Cependant, je ne suis pas sûre que ce soit un gros bidouillage. Si quelqu'un à
    plus d'explication et/ou une solution plus élégante, je suis preneuse !

Discussions similaires

  1. Réponses: 4
    Dernier message: 07/03/2014, 15h16
  2. Sérialisation d'une Hashtable
    Par dolsky dans le forum VB.NET
    Réponses: 1
    Dernier message: 29/01/2009, 01h18
  3. [HashMap] Modification d'objet d'une Hashtable
    Par viena dans le forum Collection et Stream
    Réponses: 6
    Dernier message: 29/07/2004, 09h04
  4. [JSP][STRUTS]Cle d'une hashtable
    Par julienOriano dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 23/06/2004, 13h47

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