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 :

Copier / coller JFileChooser - JList


Sujet :

Entrée/Sortie Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Par défaut Copier / coller JFileChooser - JList
    Bonjour,

    Je commence à sélectionner un fichier ou plus via un jfilechooser et l'ajoute ) un JList avec les fonctions suivantes :
    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
     
    public void ajouterElement(String file) { 
    		model.addElement(file);
    	}
     
    	public void addSelectionListener(ListSelectionListener listener) {
    		lstSelectionnerUnFichier.getSelectionModel().addListSelectionListener(listener);
    	}
     
     
    private static Iterable<String> getFiles(Component parent) {
    	    JFileChooser fileChooser = new JFileChooser();
    	  //changement du nom du bouton ouvrir
    		fileChooser.setApproveButtonText("Ajouter le(s) fichier(s) é la liste...");
    		//changement du titre
    		fileChooser.setName("Sélection de fichier(s)");
    		// sélectionner uniquement des fichiers
    		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    	    //sélectionner plusieurs fichiers à la fois
    		fileChooser.setMultiSelectionEnabled(true);
    	    //ouverture de la boite de dialogue
    		fileChooser.showOpenDialog(parent);
    	    //récupérer les fichiers sélectionnés
    		File[] files = fileChooser.getSelectedFiles();
    	    if ( files!=null ) {
    	        ArrayList<String> names = new ArrayList<String>(files.length);
    		for(File f : files) {
    		    //récupérer le nom des fichiers
    			names.add(f.getName());	
    		}
    		return names;
    	    }
    	    else {
    	        return Collections.emptyList();
    	    }
    	}
    et j'ai trouver le code suivante pour copier un fichier :
    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
     
    private boolean CopierFichier(File Source, File Destination){
            boolean resultat=false;
            FileInputStream filesource=null;
            FileOutputStream fileDestination=null;
            try{
                filesource=new FileInputStream(Source);
                fileDestination=new FileOutputStream(Destination);
                byte buffer[]=new byte[512*1024];
                int nblecture;
                while((nblecture=filesource.read(buffer))!=-1){
                    fileDestination.write(buffer,0,nblecture);
                }
                resultat=true;
            }catch(FileNotFoundException nf){
                nf.printStackTrace();
            }catch(IOException io){
                io.printStackTrace();
            }finally{
                try{
                    filesource.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
                try{
                    fileDestination.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            } 
            return resultat;
        }
    Petit coup de pouce svp, MERCI d'avance

  2. #2
    Membre confirmé
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Par défaut
    j'ai oublié de préciser que le fichier dans être coller dans un sous dossier.
    Voici le code qui crée les dossiers :
    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
     
     
    package pSignature.pInclude;
     
    import java.io.File;
     
    import pSignature.fSignature;
     
    public class Repertoire
    {
    	private static String reference_indice;
    	private static String reference_409_indice_gz;
    	private static String folderName;
    	private static String filePath; 
    	private static String chemin;
     
    	public static void concepteurData(Object reference, Object indice)
    	{
    		//creer le dossier parent
    		reference_indice = reference + "_" + indice;
    		//creer le sous dossier
    		reference_409_indice_gz = reference + "_409_" + indice + "_gz";
    		//chemin du sous dossier
    		chemin = reference_indice + "/" + reference_409_indice_gz;
    		//vérification avant la création 
    		if(!new File(reference_indice).exists())
    		{
    			new File(reference_indice.toUpperCase()).mkdirs();
     
    			System.out.println("Le dossier " + reference_indice.toUpperCase() + " à bien été créé");
            } else
            	System.out.println("Le dossier " + reference_indice.toUpperCase() + " existe déjà");
     
    		if (!new File(chemin).exists())
    		{
    			new File(chemin.toUpperCase()).mkdirs();
    			//créer le fichier corespondant au dossier
                            //ce fichier est à la même auteur que le dossier
    			folderName = reference_409_indice_gz.toUpperCase();
    			filePath = reference_indice + "/" + folderName.toUpperCase() + ".txt";
    			fSignature.creerFichierText409(folderName, filePath);
                            //c'est dans ce sous dossier que je vais mettre les fichier sélectionnés de ma JList
    		} else
    			System.out.println("Le fichier " + folderName.toUpperCase() + " existe déjà dans " + filePath.toUpperCase());
    		//copier les fichiers sélectionnés dans le sous dossier
    		String collerFilePath = chemin + "/" + folderName + ".txt";
     
    		fSignature.copierFichier(collerFilePath);
    	}
    }
    Encore merci pour votre aide...

  3. #3
    Membre Expert
    Avatar de eulbobo
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2003
    Messages
    786
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2003
    Messages : 786
    Par défaut
    Citation Envoyé par nadong Voir le message
    Encore merci pour votre aide...
    Bonjour

    C'est quoi la question/le problème/ce qui ne marche pas?


    Sinon, en Java7 au moins, tu peux regarder la méthode Files.copy pour copier des fichiers

  4. #4
    Membre confirmé
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Par défaut
    Comment copier les fichiers de ma JList dans le sous dossier ?
    A savoir que je peux supprimer un fichier, donc je ne peux pas copier les fichier en même temps que j'ajout le fichier dans la JList

  5. #5
    Membre confirmé
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Par défaut
    J'ai essayer de faire 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
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    public static boolean copierFichier(String Destination){
    		String Source = null;
    		Object fichiers = lstSelectionnerUnFichier.getSelectedValue();
    		//for(String fichier : fichiers) {
    			Source = (String) fichiers;
    		//}
    		boolean resultat=false;
            FileInputStream filesource=null;
            FileOutputStream fileDestination=null;
            try{
                filesource=new FileInputStream(Source);
                fileDestination=new FileOutputStream(Destination);
                byte buffer[]=new byte[512*1024];
                int nblecture;
                while((nblecture=filesource.read(buffer))!=-1){
                    fileDestination.write(buffer,0,nblecture);
                }
                resultat=true;
            }catch(FileNotFoundException nf){
                nf.printStackTrace();
            }catch(IOException io){
                io.printStackTrace();
            }finally{
                try{
                    filesource.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
                try{
                    fileDestination.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            } 
            return resultat;
        }
    Et voici l'erreur :
    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
     
    Le dossier 01976482_-A à bien été créé
    Le fichier 01976482_409_-A_GZ a été créé !
    java.io.FileNotFoundException: mise_en_approbation.tcl (Le fichier spécifié est introuvable)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at pSignature.fSignature.copierFichier(fSignature.java:475)
    	at pSignature.pInclude.Repertoire.concepteurData(Repertoire.java:45)
    	at pSignature.fSignature.validation(fSignature.java:415)
    	at pSignature.fSignature$2.actionPerformed(fSignature.java:127)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    java.lang.NullPointerException
    	at pSignature.fSignature.copierFichier(fSignature.java:489)
    	at pSignature.pInclude.Repertoire.concepteurData(Repertoire.java:45)
    	at pSignature.fSignature.validation(fSignature.java:415)
    	at pSignature.fSignature$2.actionPerformed(fSignature.java:127)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    java.lang.NullPointerException
    	at pSignature.fSignature.copierFichier(fSignature.java:494)
    	at pSignature.pInclude.Repertoire.concepteurData(Repertoire.java:45)
    	at pSignature.fSignature.validation(fSignature.java:415)
    	at pSignature.fSignature$2.actionPerformed(fSignature.java:127)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Le fichier 01976482_-A.txt a été créé !
    Le fichier support.txt a été créé !
    Pourquoi il ne trouve pas le fichier ?

  6. #6
    Membre Expert
    Avatar de eulbobo
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2003
    Messages
    786
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2003
    Messages : 786
    Par défaut
    Parce que tu ne lui donne que le nom...

    Sachant que tu récupères déjà des fichiers
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    File[] files = fileChooser.getSelectedFiles();
    Pourquoi tu ne gardes pas tes objets fichiers? Ils ont tout ce qu'il faut pour permettre de les manipuler simplement !


    Et je répète, pour copier un fichier, maintenant la bonne méthode, c'est d'utiliser la méthode Files.copy ! (ça évite de réinventer la roue sur des trucs et de se concentrer sur la logique de SON application)

Discussions similaires

  1. [VB.NET] copier coller d'une valeur
    Par mic56 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/06/2004, 11h43
  2. [Swing]copier coller... dans le menu.
    Par parksto dans le forum Composants
    Réponses: 3
    Dernier message: 10/05/2004, 22h56
  3. Copier coller Fichier windows
    Par KPitN dans le forum Windows
    Réponses: 8
    Dernier message: 20/04/2004, 17h32
  4. Copier Coller une ligne d'une table avec modif ?
    Par nolan76 dans le forum Requêtes
    Réponses: 4
    Dernier message: 04/03/2004, 16h34
  5. [Débutant] Conserver la couleur lors d'un copier coller
    Par ADIDASman dans le forum JBuilder
    Réponses: 2
    Dernier message: 21/05/2003, 18h13

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