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 :

lecture et modification fichier


Sujet :

Entrée/Sortie Java

  1. #1
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut lecture et modification fichier
    salut,
    j'ai écrit un programme qui me permet de lire un fichier et de faire des modification, et pour modifier le contenu j'ai utiliser un tableau ou je stocke les ligne puis je modifie la case correspondant et je réécrit mon 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
    33
    34
     
    //--modifier dat 
    		// 1ière étape lecture du dat
    		String[] x=new String[7];//<-- nombre de ligne
     
    		 try {
    			   FileInputStream ips=new FileInputStream("C:\\data_spots\\getdata.dat");
    			   InputStreamReader ipsr=new InputStreamReader(ips);
    			   BufferedReader br=new BufferedReader(ipsr);
    			   String ligne;
    			   int y = 0;
    			   System.out.println(br.readLine());
     
     
    			   while ((ligne=br.readLine())!=null ) {
    			    String text = ligne;
    			    x[y]=text;
    			    y++;
    			   }
    			   br.close();
    		 }
    			  catch (Exception e) {
    			   System.out.println(e.toString());
    			  } 
     
     
    			  }
    			  x[3]= "lcd C:\\data_spots";
     
     
    			  for (int i=0;i<x.length;i++) {
     
    				  System.out.println(x[i]);
    			  }
    je ne veux pas utiliser le tableau et j'aimerais utiliser FileOutputStream
    si vous pouvez changer mon code j'en serais reconnaissant

  2. #2
    Membre chevronné
    Profil pro
    Fabrication GED
    Inscrit en
    Octobre 2005
    Messages
    1 405
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Fabrication GED

    Informations forums :
    Inscription : Octobre 2005
    Messages : 1 405
    Points : 1 958
    Points
    1 958
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    while ((ligne=br.readLine())!=null ) {
      if(y==3){
        System.out.println("lcd C:\\data_spots");
      }else{
        System.out.println(ligne);
      }
      ... // la suite 
    }

  3. #3
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    merci,
    en fait je veux changer le contenu de la 3ième ligne dans le fichier et pas l'afficher seulement.
    comment faire ?

  4. #4
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    Bonjour,

    Tu es obligé de liretout le fichier, de faire toutes les modifications que tu veux puis de réecrire le fichier. Autrement dit tu modifies la case 2 de ton tableau de String ensuite tu réecris le contenu du tableau dans un fichier du mème nom, ce qui aura pour effet d'écraser l'ancien contenu au profit du nouveau ( avec la ligne modifiée ) .

    Voilà, j'éspère avoir été clair. Si tu rencontres des problèmes n'hésite pas à poser des questions

    @+

  5. #5
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    Citation Envoyé par afrikha
    Bonjour,

    Tu es obligé de liretout le fichier, de faire toutes les modifications que tu veux puis de réecrire le fichier. Autrement dit tu modifies la case 2 de ton tableau de String ensuite tu réecris le contenu du tableau dans un fichier du mème nom, ce qui aura pour effet d'écraser l'ancien contenu au profit du nouveau ( avec la ligne modifiée ) .

    Voilà, j'éspère avoir été clair. Si tu rencontres des problèmes n'hésite pas à poser des questions

    @+
    le problème est que je ne veux plus utilisé un tableau !
    je demandais si je peux modifier "br.readLine()" ?
    voici mon code complet
    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
     
    //--modifier dat 
    		// 1ière étape lecture du bat
    		String[] x=new String[7];//<-- nombre de ligne
     
    		 try {
    			   FileInputStream ips=new FileInputStream("C:\\data_spots\\getdata.dat");
    			   InputStreamReader ipsr=new InputStreamReader(ips);
    			   BufferedReader br=new BufferedReader(ipsr);
    			   String ligne;
    			   int y = 0;
     
     
     
    			   while ((ligne=br.readLine())!=null ) {
    			    String text = ligne;
    			    x[y]=text;
    			    y++;
    			   }
    			   br.close();
    		 }
    			  catch (Exception e) {
    			   System.out.println(e.toString());
    			  } 
     
     
    			  x[3]= "lcd C:\\data_spots;
     
     
     
     
     
               //-- 2ième étape reécrir dans mon .bat
    			  PrintWriter ecrivain;
    			  ecrivain =  new PrintWriter(new BufferedWriter
    					  (new FileWriter("C:\\data_spots\\getdata.dat")));
    			  for (int i=0;i<x.length;i++) {
    				  ecrivain.println(x[i]);
    			  }
       ecrivain.close();

  6. #6
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    Citation Envoyé par wiss20000
    le problème est que je ne veux plus utilisé un tableau !
    Tu veux utiliser quoi alors ? une ArrayList<String> ?...
    je demandais si je peux modifier "br.readLine()" ?
    Tu ne peux pas faire de modifications directement dans le fichier, à moins que j'ai mal compris ce que tu souhaite faire...
    Bref détaille un peu plus pourqu'on puisse t'aider.

    @+

  7. #7
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    c'est simple je veux modifié une ligne de mon fichier (pour ce fait je ne veux pas utiliser un tableau pour stocker les différentes ligne de mon fichier lors de la lecture comme c'est le cas pour mon code)

  8. #8
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    à part lire le fichier en entier, faire les modifications puis le réecrire, il n'y a pas d'autres alternatives pour modifier un fichier...

    @+

  9. #9
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    en fait quand j'ai utilisé ce code une érreur c'est produite: à la réecriture du fichier la première ligne est bizzarement supprimer

  10. #10
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    Fais-voir ton code ainsi que l'erreure obtenue.
    On est pas devin...

    @+

  11. #11
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    Citation Envoyé par afrikha
    Fais-voir ton code ainsi que l'erreure obtenue.
    On est pas devin...

    @+
    voici mon code entier
    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
     
    import java.io.*;
    import java.util.Date;
    import java.util.Locale;
    import java.text.DateFormat;
     
     
    public class creerdossier {
    	public static void main(String[] args) throws IOException, InterruptedException {
     
    		Date dt= new java.util.Date();
    		Locale locale = Locale.getDefault();
    		/** 2. Construction du DateFormat en choisiant un format :
                     * SHORT = 29/03/2007
                     * FULL = jeudi 29 mars 2007
                     */
    		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
    		System.out.println(dateFormat.format(dt));
    		String str = dateFormat.format(dt).replace(' ','_');
    		//System.out.println(str);
    		File fb = new File("C:\\data_spots\\"+str+""); 
    		fb.mkdirs();
     
    		//--modifier dat 
    		// 1ière étape lecture du dat
    		String[] x=new String[7];//<-- nombre de ligne
     
    		 try {
    			   FileInputStream ips=new FileInputStream("C:\\data_spots\\getdata.dat");
    			   InputStreamReader ipsr=new InputStreamReader(ips);
    			   BufferedReader br=new BufferedReader(ipsr);
    			   String ligne;
    			   int y = 0;
    			   System.out.println(br.readLine());
     
     
    			   while ((ligne=br.readLine())!=null ) {
    			    String text = ligne;
    			    x[y]=text;
    			    y++;
    			   }
    			   br.close();
    		 }
    			  catch (Exception e) {
    			   System.out.println(e.toString());
    			  } 
     
    			  x[3]= "lcd C:\\data_spots\\"+str+"";
     
     
               //-- 2ième étape reécrir dans mon .bat
    			  PrintWriter ecrivain;
    			  ecrivain =  new PrintWriter(new BufferedWriter
    					  (new FileWriter("C:\\data_spots\\getdata.dat")));
    			  for (int i=0;i<x.length;i++) {
    				  ecrivain.println(x[i]);
    			  }
       ecrivain.close();
     
    	}
    }
    le problème est que lors de la réécriture du fichier la première ligne est supprimer

  12. #12
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    Il y a un System.out.println(br.readLine()) qui traine. Supprime-le et tout marchera mieux.

    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
     
    import java.io.*;
    import java.util.Date;
    import java.util.Locale;
    import java.text.DateFormat;
     
     
    public class creerdossier {
    	public static void main(String[] args) throws IOException, InterruptedException {
     
    		Date dt= new java.util.Date();
    		Locale locale = Locale.getDefault();
    		/** 2. Construction du DateFormat en choisiant un format :
                     * SHORT = 29/03/2007
                     * FULL = jeudi 29 mars 2007
                     */
    		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
    		System.out.println(dateFormat.format(dt));
    		String str = dateFormat.format(dt).replace(' ','_');
    		//System.out.println(str);
    		File fb = new File("C:\\data_spots\\"+str+""); 
    		fb.mkdirs();
     
    		//--modifier dat 
    		// 1ière étape lecture du dat
    		String[] x=new String[7];//<-- nombre de ligne
     
    		 try {
    			   FileInputStream ips=new FileInputStream("C:\\data_spots\\getdata.dat");
    			   InputStreamReader ipsr=new InputStreamReader(ips);
    			   BufferedReader br=new BufferedReader(ipsr);
    			   String ligne;
    			   int y = 0;
    			   //System.out.println(br.readLine());<-- ici tu consommes la prmière ligne
     
     
    			   while ((ligne=br.readLine())!=null ) {
    			    String text = ligne;
    			    x[y]=text;
    			    y++;
    			   }
    			   br.close();
    		 }
    			  catch (Exception e) {
    			   System.out.println(e.toString());
    			  } 
     
    			  x[3]= "lcd C:\\data_spots\\"+str+"";
     
     
               //-- 2ième étape reécrir dans mon .bat
    			  PrintWriter ecrivain;
    			  ecrivain =  new PrintWriter(new BufferedWriter
    					  (new FileWriter("C:\\data_spots\\getdata.dat")));
    			  for (int i=0;i<x.length;i++) {
    				  ecrivain.println(x[i]);
    			  }
       ecrivain.close();
     
    	}
    }
    @+

  13. #13
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    Citation Envoyé par afrikha
    Il y a un System.out.println(br.readLine()) qui traine. Supprime-le et tout marchera mieux.

    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
     
    import java.io.*;
    import java.util.Date;
    import java.util.Locale;
    import java.text.DateFormat;
     
     
    public class creerdossier {
    	public static void main(String[] args) throws IOException, InterruptedException {
     
    		Date dt= new java.util.Date();
    		Locale locale = Locale.getDefault();
    		/** 2. Construction du DateFormat en choisiant un format :
                     * SHORT = 29/03/2007
                     * FULL = jeudi 29 mars 2007
                     */
    		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
    		System.out.println(dateFormat.format(dt));
    		String str = dateFormat.format(dt).replace(' ','_');
    		//System.out.println(str);
    		File fb = new File("C:\\data_spots\\"+str+""); 
    		fb.mkdirs();
     
    		//--modifier dat 
    		// 1ière étape lecture du dat
    		String[] x=new String[7];//<-- nombre de ligne
     
    		 try {
    			   FileInputStream ips=new FileInputStream("C:\\data_spots\\getdata.dat");
    			   InputStreamReader ipsr=new InputStreamReader(ips);
    			   BufferedReader br=new BufferedReader(ipsr);
    			   String ligne;
    			   int y = 0;
    			   //System.out.println(br.readLine());<-- ici tu consommes la prmière ligne
     
     
    			   while ((ligne=br.readLine())!=null ) {
    			    String text = ligne;
    			    x[y]=text;
    			    y++;
    			   }
    			   br.close();
    		 }
    			  catch (Exception e) {
    			   System.out.println(e.toString());
    			  } 
     
    			  x[3]= "lcd C:\\data_spots\\"+str+"";
     
     
               //-- 2ième étape reécrir dans mon .bat
    			  PrintWriter ecrivain;
    			  ecrivain =  new PrintWriter(new BufferedWriter
    					  (new FileWriter("C:\\data_spots\\getdata.dat")));
    			  for (int i=0;i<x.length;i++) {
    				  ecrivain.println(x[i]);
    			  }
       ecrivain.close();
     
    	}
    }
    @+
    merci ça marhe
    et à propos de l'utilisation du tableau il y pas d'autre solution ?
    car on m'a dis que je peux utiliser "FileOutputStream" et "readLine() " aulieux du tab

  14. #14
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    Citation Envoyé par wiss20000
    et à propos de l'utilisation du tableau il y pas d'autre solution ?
    car on m'a dis que je peux utiliser "FileOutputStream" et "readLine() " aulieux du tab
    Je ne comprends trop ce que tu veux dire, désolé. Peux-tu reformuler autrement ta question ? tu souhaites utiliser quoi à la place du tableau ? est-ce que tu veux écrire directement ce que tu lis sans passer par un tableau ? bref donne-nous plus de précisions...

    @+

  15. #15
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    je veux dire : est ce je peux juste modifier une ligne bien précise de mon fichier sans avoir recours à lire tout le fichier ?

  16. #16
    Membre chevronné
    Avatar de afrikha
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    1 600
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 600
    Points : 2 208
    Points
    2 208
    Par défaut
    Citation Envoyé par wiss20000
    je veux dire : est ce je peux juste modifier une ligne bien précise de mon fichier sans avoir recours à lire tout le fichier ?
    non !

  17. #17
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 109
    Points : 57
    Points
    57
    Par défaut
    Moi j'utilise ça :
    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
     
    public void lecture(){		
    		try {
    			BufferedReader bufRead = new BufferedReader(new FileReader("tonfichier"));	
    			//Lecture ligne par ligne du flux
     
    			String reponse = bufRead.readLine();
    			String reponse1 = bufRead.readLine();
    			String reponse2 = bufRead.readLine();
    			String reponse3 = bufRead.readLine();
    			System.out.println(reponse1+"\n"+reponse2+"\n"+reponse3+"\n");
     
    			} catch (IOException e) {
    				System.out.println("Erreur dans l'ecriture du fichier trame.txt");
    			}
    	}
    Avec reponse ta ligne 1 et ainsi de suite!

  18. #18
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    Alors je suppose que la solution est qu'il n y a pas d'autre solution

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

Discussions similaires

  1. lecture et modification fichier
    Par alaninho dans le forum Linux
    Réponses: 1
    Dernier message: 19/04/2012, 16h46
  2. Lecture et modification d'un fichier
    Par walacouper dans le forum z/OS
    Réponses: 1
    Dernier message: 27/05/2009, 23h58
  3. Réponses: 2
    Dernier message: 24/11/2008, 10h53
  4. Réponses: 50
    Dernier message: 19/10/2007, 23h38

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