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

avec Java Discussion :

Erreur de compilation (Cannot find sympbol method writeObject)


Sujet :

avec Java

  1. #1
    Membre régulier
    Homme Profil pro
    Inscrit en
    Janvier 2008
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 306
    Points : 106
    Points
    106
    Par défaut Erreur de compilation (Cannot find sympbol method writeObject)
    Bonjour,

    Le code suivant créer une erreur de compilation, pourquoi ?

    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
     
    import java.util.*;
    import java.io.*;
     
     
    class Patient
    {
     
    private String nom;
    private String prenom;
     
    public Patient(String nom, String prenom)
    	{
    	this.nom = nom;
    	this.prenom = prenom;
    	}
     
    }
     
     
    class Fichier implements Serializable
    {
     
    private ObjectInputStream ofR;
     
    public Fichier(String fileName) throws IOException
    	{
    	String nomDuFichier = fileName + ".dat";
    	ofR = new ObjectInputStream(new FileInputStream(nomDuFichier));
    	}
     
    public void ecrire(Patient chaine) throws IOException
    	{
    	ofR.writeObject("chaine");
    	}
     
    public void fermer() throws IOException
    	{
    	ofR.close();
    	}
     
    }
     
     
    class Titeuf
    {
     
    	public static void main(String[] arg)
    	{
    		try
    		{
     
    		Patient bernard;
    		bernard.nouveau("DUPOND", "Bernard");
     
    		Scanner lireClavier = new Scanner(System.in);
    		System.out.print("Saisir le nom du fichier : ");
    		String nomFichier = lireClavier.nextLine();
     
    		Fichier fichier1 = new Fichier("nomFichier");
    		fichier1.ecrire(bernard);
     
    		}
    		catch(IOException e)
    		{
    		System.out.println(e.getMessage());
    		}
    		finally
    		{
    		fichier1.fermer();
    		}
     
    	}
     
     
     
    }
    Merci d'avance pour vos réponses,
    Cordialement

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Un ObjectInputStream sert à lire, pas a écrire, il n'a donc aucune méthode de write.

  3. #3
    Membre régulier
    Homme Profil pro
    Inscrit en
    Janvier 2008
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 306
    Points : 106
    Points
    106
    Par défaut
    Salut,

    Merci pour ta réponse,
    J'ai fait les modifications necessaires et je pense que tout fonctionne bien, cela dit lorsque je mets fichier1.close() dans le bloc finally il me dit "cannot find symbol fichier1" alors qu'en le mettant a la suite du code dans le bloc try tout fonctionne bien.

    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
    import java.util.*;
    import java.io.*;
     
     
    class Patient
    {
     
    private String nom;
    private String prenom;
     
    public Patient(String nom, String prenom)
    	{
    	this.nom = nom;
    	this.prenom = prenom;
    	}
     
    }
     
     
    class Fichier implements Serializable
    {
     
    private ObjectOutputStream ofR;
    private String fileName;
     
    public Fichier(String fileName) throws IOException
    	{
    	String nomDuFichier = fileName + ".dat";
    	ofR = new ObjectOutputStream(new FileOutputStream(nomDuFichier));
    	}
     
    public void ecrire(Patient chaine) throws IOException
    	{
    	ofR.writeObject("chaine");
    	}
     
    public void fermer() throws IOException
    	{
    	ofR.close();
    	}
     
    }
     
     
    class Titeuf
    {
     
    	public static void main(String[] arg)
    	{
    		try
    		{
     
    		Patient bernard = new Patient("DUPOND", "Bernard");
     
    		Scanner lireClavier = new Scanner(System.in);
    		System.out.print("Saisir le nom du fichier : ");
    		String nomFichier = lireClavier.nextLine();
     
    		Fichier fichier1 = new Fichier(nomFichier);
    		fichier1.ecrire(bernard);
    		fichier1.fermer();
    		}
    		catch(IOException e)
    		{
    		System.out.println(e.getMessage());
    		}
    		finally
    		{
    		//fichier1.fermer();
    		}
     
    	}
     
     
     
    }
    Merci d'avance pour ta réponse

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Parce que fichier1 est déclaré dans le bloc, pas en dehors (cf la portée des variables)

    le plus propre est comme ceci

    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
    public static void main(String[] arg)
    	{
    		try
    		{
     
    		Patient bernard = new Patient("DUPOND", "Bernard");
     
    		Scanner lireClavier = new Scanner(System.in);
    		System.out.print("Saisir le nom du fichier : ");
    		String nomFichier = lireClavier.nextLine();
     
    		Fichier fichier1 = new Fichier(nomFichier);
    		try{
    		    fichier1.ecrire(bernard);
    		} finally {
    		    fichier1.fermer();
    		}
    		catch(IOException e)
    		{
    		System.out.println(e.getMessage());
    		}
     
    	}

Discussions similaires

  1. Compilation : cannot find symbol
    Par Rhadamenthys dans le forum Général Java
    Réponses: 1
    Dernier message: 20/11/2012, 11h21
  2. Probleme a la compilation "cannot find symbol"
    Par nacrooks dans le forum Général Java
    Réponses: 4
    Dernier message: 20/01/2010, 15h16
  3. Problème compilation : Cannot find -lglut32.lib
    Par frogway dans le forum Code::Blocks
    Réponses: 0
    Dernier message: 31/01/2009, 20h17
  4. Réponses: 1
    Dernier message: 23/01/2008, 15h35
  5. Erreur après compilation : cannot execute binary file
    Par Djo00 dans le forum Administration système
    Réponses: 8
    Dernier message: 21/07/2006, 16h26

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