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 :

Ecriture dans le fichier


Sujet :

Entrée/Sortie Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 174
    Points : 51
    Points
    51
    Par défaut Ecriture dans le fichier
    Bonjour,

    Je veut écrire dans un fichier le problème c'est que quand j’exécute mon code je clique sur le bouton send il le reconnait avec l'ActionListner mais rien ne s'écrit

    Main.java

    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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.java.main;
     
    import com.java.Interface.Communica;
    import com.java.Interface.Communica2;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Hp
     */
    public class ComunicaSystem {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
     
            try {
     
                File a2b = new File("C:/Users/Hp/Desktop/SIGLIS M1/A2B.txt");
     
                File b2a = new File("C:/Users/Hp/Desktop/SIGLIS M1/B2A.txt");
     
     
                Communica comm = new Communica(new BufferedReader(new FileReader(b2a)),new BufferedWriter(new FileWriter(a2b)));
     
                //Communica2 comm2 = new Communica2(new BufferedReader(new FileReader(a2b)),new BufferedWriter(new FileWriter(b2a)));
     
            } catch (IOException ex) {
     
                ex.printStackTrace();
     
            }
        }
     
    }
    Communica.java

    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
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.java.Interface;
     
     
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
     
     
    /**
     *
     * @author Hamza Elyoubi
     * @Date : 15/11/2017
     * @Version 1.0
     */
    public class Communica extends JFrame implements WindowListener{
     
        private BufferedWriter writer;
        private BufferedReader reader;
        private JButton bReceive;
        private JButton bSend;
        private JLabel lmessrec;
        private JLabel lmesssend;
        private JTextArea textRec;
        private JTextArea textTosend;
     
     
        public Communica(BufferedReader r,BufferedWriter w){
     
             this.reader = r;
            this.writer = w;
            initComponents();
     
        }
     
     
     
     
        private void initComponents(){
     
            this.setAlwaysOnTop(true);
     
            setTitle("Communication window");
     
            lmesssend = new JLabel("Message to send : ");
     
            lmessrec = new JLabel("Received message : ");
     
            textTosend = new JTextArea(7, 20);
     
            JScrollPane scrollPaneTosend = new JScrollPane(textTosend);
     
            textRec = new JTextArea(7, 20);
     
            textRec.setEditable(false);
     
            JScrollPane scrollPaneToreceive = new JScrollPane(textRec);
     
            bSend = new JButton("Send");
     
            bReceive = new JButton("Receive");
     
            setSize(500, 300);
     
            //Trois lignes sur deux colonnes avec GridLayout
            setLayout(new GridLayout(3, 2));
     
            //On ajoute chaque composant au contentPane
            getContentPane().add(lmesssend);
            getContentPane().add(scrollPaneTosend);
            getContentPane().add(bSend);
            getContentPane().add(bReceive);
            getContentPane().add(lmessrec);
            getContentPane().add(scrollPaneToreceive);
     
     
            bSend.addActionListener(new ActionListenerSend());
     
            bReceive.addActionListener(new ActionListenerReceive());
     
     
            // the JFrame is visible now
            setVisible(true);
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
     
        }
     
     
     
        public class ActionListenerSend implements ActionListener{
     
            public void actionPerformed(ActionEvent event){
     
            try {
     
     
                writer.write(textTosend.getText());
     
                System.out.println("ecriture faite");
     
            } catch (IOException ex) {
     
              	ex.printStackTrace();
     
            } 
          }
        }
     
        public class ActionListenerReceive implements ActionListener{
     
            public void actionPerformed(ActionEvent event){
     
            try {
     
            String text = "";
     
            String line = reader.readLine();
     
                while (line!=null) {
     
                    text += line;
     
                    line = reader.readLine();
     
                }
     
            textRec.setText(text);
     
            } catch (Exception e) {
     
                e.printStackTrace();
     
            }    
     
            }
        }
     
     
     
        @Override
        public void windowClosed(WindowEvent e) {
     
            try {
     
                if (writer != null){
     
                    writer.close();
     
                }
                if (reader != null){
     
                    reader.close();
     
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
     
     
        }
     
     
        @Override
        public void windowOpened(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowClosing(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowIconified(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowDeiconified(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowActivated(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowDeactivated(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
    }

  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
    Vu que tu bufferise, oublie pas de faire un flush après ton écriture.

    Pour le reste, faudra que tu précise. On parle de quel actionlistener?
    Rien ne s'écrit, mais où? Dans le fichier? à l'écran?
    Des exceptions dans la console?

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 174
    Points : 51
    Points
    51
    Par défaut
    Bonjour,

    Merci effectivement il manquait le flush pour que ça marche, j'ai une dernière chose, je veux aussi garder les anciens messages afficher dans le text-aeria de réception (en bas) que j'écris dans le text-aeria d'en haut et que je send , je veux en gros qu'ont chaque fois les messages s'accumule au fur et à mesure que j'écris et que j'efface mes messages..

    je remet mon code mis a jour :

    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
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.java.Interface;
     
     
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
     
     
    /**
     *
     * @author Hamza Elyoubi
     * @Date : 15/11/2017
     * @Version 1.0
     */
    public class Communica extends JFrame implements WindowListener{
     
        private BufferedWriter writer;
        private BufferedReader reader;
        private JButton bReceive;
        private JButton bSend;
        private JLabel lmessrec;
        private JLabel lmesssend;
        private JTextArea textRec;
        private JTextArea textTosend;
     
     
        public Communica(BufferedReader r,BufferedWriter w){
     
             this.reader = r;
            this.writer = w;
            initComponents();
     
        }
     
     
     
     
        private void initComponents(){
     
            this.setAlwaysOnTop(true);
     
            setTitle("Communication window");
     
            lmesssend = new JLabel("Message to send : ");
     
            lmessrec = new JLabel("Received message : ");
     
            textTosend = new JTextArea(7, 20);
     
            JScrollPane scrollPaneTosend = new JScrollPane(textTosend);
     
            textRec = new JTextArea(7, 20);
     
            textRec.setEditable(false);
     
            JScrollPane scrollPaneToreceive = new JScrollPane(textRec);
     
            bSend = new JButton("Send");
     
            bReceive = new JButton("Receive");
     
            setSize(500, 300);
     
            //Trois lignes sur deux colonnes avec GridLayout
            setLayout(new GridLayout(3, 2));
     
            //On ajoute chaque composant au contentPane
            getContentPane().add(lmesssend);
            getContentPane().add(scrollPaneTosend);
            getContentPane().add(bSend);
            getContentPane().add(bReceive);
            getContentPane().add(lmessrec);
            getContentPane().add(scrollPaneToreceive);
     
     
            bSend.addActionListener(new ActionListenerSend());
     
            bReceive.addActionListener(new ActionListenerReceive());
     
     
            // the JFrame is visible now
            setVisible(true);
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
     
        }
     
     
     
        public class ActionListenerSend implements ActionListener{
     
            public void actionPerformed(ActionEvent event){
     
            try {
     
     
                writer.write(textTosend.getText());
     
                writer.flush();
     
     
            } catch (IOException ex) {
     
              	ex.printStackTrace();
     
            } 
          }
        }
     
        public class ActionListenerReceive implements ActionListener{
     
            public void actionPerformed(ActionEvent event){
     
            try {
     
            String text = "";
     
            String line = reader.readLine();
     
                while (line!=null) {
     
                    text += line;
     
                    line = reader.readLine();
     
                }
     
            textRec.setText(text);
     
            } catch (Exception e) {
     
                e.printStackTrace();
     
            }    
     
            }
        }
     
     
     
        @Override
        public void windowClosed(WindowEvent e) {
     
            try {
     
                if (writer != null){
     
                    writer.close();
     
                }
                if (reader != null){
     
                    reader.close();
     
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
     
     
        }
     
     
        @Override
        public void windowOpened(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowClosing(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowIconified(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowDeiconified(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowActivated(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void windowDeactivated(WindowEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
    }
    merci

  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
    Il suffit simplement que tu récupère l'ancien texte et que tu y ajoute le nouveau.

Discussions similaires

  1. probleme d'ecriture dans un fichier texte
    Par azrael88370 dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 24/01/2005, 17h33
  2. Script shell - écriture dans un fichier
    Par Leishmaniose dans le forum Linux
    Réponses: 6
    Dernier message: 13/12/2004, 22h48
  3. Probleme d'ecriture dans un fichier en mode Text
    Par bbozet dans le forum C++Builder
    Réponses: 3
    Dernier message: 14/01/2004, 13h46
  4. [LG]ecriture dans un fichier binaire
    Par jsaviola dans le forum Langage
    Réponses: 3
    Dernier message: 26/12/2003, 17h30
  5. Réponses: 2
    Dernier message: 26/09/2003, 14h51

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