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 + creation fichier.txt


Sujet :

Entrée/Sortie Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    177
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 177
    Points : 74
    Points
    74
    Par défaut ecriture + creation fichier.txt
    Salut à tous,
    Je dois écrire une class en java qui prend en parametre une arrayList de tableau de String et qui ecrit le contenu de cette arrayList dans un fichier.txt.
    Je debute en java si quelqu'un pourrait me donner des pistes
    Merci à tous

  2. #2
    Membre éclairé Avatar de herch
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    655
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2006
    Messages : 655
    Points : 773
    Points
    773
    Par défaut
    qu'est-ce qui te pose problème?? le parcours de l'arraylist ou l'écriture dans le fichier texte??

    pour les entrées/sorties en général, tu peux voir ce cours : http://skoffler.developpez.com/tutoriels/javaSE/ES

    pour la lecture/écriture d'un flux de caractères :
    http://skoffler.developpez.com/tutor.../?page=page_13
    http://anisfrikha.developpez.com/tut...io/#LIII-B-2-b

    pour les arraylist : http://fmora.developpez.com/tutoriel...oduction/#L3.2

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    177
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 177
    Points : 74
    Points
    74
    Par défaut suite
    Et bien en fait je creer mon fichier.txt

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    File f = new File(path+""+nomFichier+""+extension);
    		f.createNewFile();
    et j'ai mon ArrayList de String:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ArrayList<String[]> list = new ArrayList<String[]>();
    Mais je ne sais pas comment:
    * insérer mes caractères dans mon tableau de String
    *insérer chaque tableau dans une case de mon ArrayList.
    *écrire le contenue de mon ArrayList dans mon fichier.txt crée

  4. #4
    Membre éclairé Avatar de herch
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    655
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2006
    Messages : 655
    Points : 773
    Points
    773
    Par défaut
    un exemple
    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
     
    String[] tab1 = {"element1tab1", "element2tab1", "element3tab1"};
    String[] tab2 = {"element1tab2", "element2tab2", "element3tab2"};
    //ajout des deux tableaux à ton arraylist
    ArrayList<String[]> list = new ArrayList<String[]>();
    list.add(tab1);
    list.add(tab2);
    //Ecriture dans le fichier texte
    PrintWriter writer = null;
    try{
    	writer = new PrintWriter(new BufferedWriter(new FileWriter("fichier.txt")));
            //On parcourt les éléments de l'ArrayList
    	for (String[] tab : list)
    	{
                    //puisque chaque élément est un tableau, on parcours ce dernier
    		for (int i = 0; i < tab.length; i++)
                            //on écrit l'élément récupéré dans le ficher texte
    			writer.println(tab[i]);
    	}
    } catch (IOException ioe) {
    	ioe.printStackTrace();
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	if (writer != null)
    		writer.close();
    }
    et regarde les liens que je t'avais proposé pour plus d'infos

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    177
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 177
    Points : 74
    Points
    74
    Par défaut suite
    Bon en même temps je gelère plutot en java voici mes deux classes à l'execution j'ai un "nullPointerException"

    Ma classe qui creer un fichier txt puis doit ecrire les données dedans:
    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
    package univcezanne.sircoulomb.creationfichier;
     
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
     
    public class ExportTxt {
     
    	protected String nomFichier;
    	protected String path;
    	private String extension;
    	protected ArrayList<String[]> list;
     
     
    	public ExportTxt(String nomFichier, String path) {
    		this.nomFichier = nomFichier;
    		this.path = path;
    		extension = ".txt";
    	}
     
    	public void creerFichier() throws IOException{
    		File f = new File(path+""+nomFichier+""+extension);
    		f.createNewFile();
    	}
     
    	public void ecritureDansFichier() throws IOException{
    		PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(path+""+nomFichier+""+extension)));
    		for (String[] tab : list)
    		{
    		    for (int i = 0; i < tab.length; i++)
    		        writer.println(tab[i]);
    		}
     
    	}
    }
    mas classe de test:
    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
    package main;
     
    import java.io.IOException;
    import java.util.ArrayList;
     
    import univcezanne.sircoulomb.creationfichier.ExportTxt;
     
    public class Test {
     
    	/**
             * @param args
             * @throws IOException 
             */
    	public static void main(String[] args) throws IOException {
    		try{
    			//Creation du fichier.txt
    			ExportTxt fichier = new ExportTxt("test2", "C:\\");
    			fichier.creerFichier();
     
    			ArrayList<String[]> list = new ArrayList<String[]>();
     
    			String tab[] = {"chaine1", "chaine2", "chaine3" , "chaine4;"};
    			String tab2[] = {"chaine1", "chaine2", "chaine3" , "chaine4;"};
     
    			//ajout des tableaux de String dans l'ArrayList
    			list.add(tab);
    			list.add(tab2);
     
     
    			//ecriture dans fichier
    			fichier.ecritureDansFichier();
     
     
    		}
    		catch(IOException e){
     
    		}
    	}
     
    }
    désoler je suis plutôt novice en la matiere

  6. #6
    Membre éclairé Avatar de Heimdal
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    549
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 549
    Points : 718
    Points
    718
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    protected ArrayList<String[]> list;
    list n'est pas instancié dans ta classe ExportTxt donc cet attribut est nul et quand tu appelles ecritureDansFichier() badaboum!
    Deux solutions:
    - soit tu ajoutes un argument au constructeur (une List<String[]>) et tu initialise list.

    - soit (et je pense que c'est mieux) tu enlèves list des attributs de classes et tu changes ta méthode public void ecritureDansFichier() en public void ecritureDansFichier(List<String[]> list)

  7. #7
    Membre éclairé Avatar de herch
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    655
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2006
    Messages : 655
    Points : 773
    Points
    773
    Par défaut
    je vote pour la 2ème solution

    une remarque
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    catch(IOException e){
     
    }
    ceci est déconseillé, car si tu as une exception, tu ne le sauras pas; donc, un printStackTrace ne fera pas de mal
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    catch(IOException e){
        e.printStackTrace();			
    }

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    177
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 177
    Points : 74
    Points
    74
    Par défaut suite
    j'ai donc opté pour la deuxieme solution soit:

    *j'ai supprimer "protected ArrayList<String[]> list;"
    *j'ai ajouter en parametre dans ma methode ecritureDansFichier "ArrayList<String[]> list"

    *j'ai effectuer dans ma class Test :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    String[] test = list.get(0);
    			System.out.println(test[0]);
    			System.out.println(test[1]);
    			System.out.println(test[2]);
    			System.out.println(test[3]);
    			String[] test2 = list.get(1);
    			System.out.println(test2[0]);
    			System.out.println(test2[1]);
    			System.out.println(test2[2]);
    			System.out.println(test2[3]);
    pour vérifier que mon ArrayList contenait bien mes chaines. Les test son concluant.

    *j'ai ajouter dans ma classe Test "fichier.ecritureDansFichier(list);"

    je ne comprend pas j'ai rien qui est ecrit dans mon fichier texte generé.

  9. #9
    Membre éclairé Avatar de herch
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    655
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2006
    Messages : 655
    Points : 773
    Points
    773
    Par défaut
    ferme ton writer

    et puis cette méthode n'a aucune utilité
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public void creerFichier() throws IOException{
    		File f = new File(path+""+nomFichier+""+extension);
    		f.createNewFile();
    	}
    puisque le FileWriter créera automatiquement le fichier s'il n'existe pas

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    177
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 177
    Points : 74
    Points
    74
    Par défaut merci
    ok ca fonctionne.
    je vous remerci pour votre aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Problème Ecriture dans fichier .txt
    Par Patatattak dans le forum VB.NET
    Réponses: 1
    Dernier message: 15/07/2011, 20h02
  2. Ecriture ds Fichier .txt en VBA
    Par jeanlouisn dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 16/12/2008, 11h21
  3. Ecriture dans fichier txt
    Par yrondi dans le forum Excel
    Réponses: 3
    Dernier message: 23/09/2008, 13h43
  4. [C++] lecture/ecriture dans fichier txt/binary
    Par vince3320 dans le forum C++
    Réponses: 6
    Dernier message: 28/09/2007, 18h00
  5. Réponses: 18
    Dernier message: 08/08/2005, 20h52

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