Fichier text et java

foued -  
 foued -
Bonjour

je veux utiliser un fichier texte de ce format.
007249;14/02/02;15.70;16.13;15.70;16.10;28904
007699;14/02/02;20.00;20.00;19.20;19.50;6340
003954;14/02/02;5.00;5.00;5.00;5.00;5

on constate que chaque ligne comporte plusieurs champ
Je programme en java
je veux donc mettre chaque champ de chaque ligne dans un tableaux a deux dimension.
mon tableaux aura donc cette form

float tableau[][] = {{007249,14/02/02,15.70,16.13,15.70,16.10,28904}
{007699;14/02/02;20.00;20.00;19.20;19.50;6340}
{003954;14/02/02;5.00;5.00;5.00;5.00;5}
}

donc il faut remplacer ; par ,
ainsi chaque ligne de ce tableaux devient une ligne de mon tableaux et chaque chanp devien un element de mon tableaux

merci

1 réponse

  1. HackTrack
     
    J'espère que ceci pourra te servir.

    Attention toutefois: tu ne pourras pas stocker ton tableau dans un float[][], car une date ne peut être stockée dans un tel tableau. Tu dois donc utiliser un String[][]. Tu devras "caster" tes données lorsque tu les liras dans le tableau. Par exemple, dans la méthode main de l'exemple ci-dessous, tu pourrais caster la première donnée en "float" en faisant ceci:
    float aData = Float.parszeFloat(data[0][6]);
    float aData = Float.parszeFloat(data[01[6]);
    float aData = Float.parszeFloat(data[2][6]);
    et les dates grâce à la classe java.util.GregorianCalendar, etc.

    import java.io.*;
    import java.util.StringTokenizer;
    /**
    *
    * @author HackTrack
    * @version 1.0
    */
    public class TableFiller {
    private String[][] datasTable;
    int width, height;

    /** Creates new TableFiller */
    public TableFiller() {
    }

    public String[][] fill(String dataFile){
    width = 0;
    height= 0;
    StringBuffer datasBuffer = new StringBuffer();
    try{
    BufferedReader reader = new BufferedReader(new FileReader(dataFile));
    String datasLine = "";
    while((datasLine=reader.readLine()) != null){
    width = (new StringTokenizer(datasLine, ";")).countTokens();
    datasBuffer.append(datasLine + ";");
    height++;
    }
    }catch(FileNotFoundException fnfe){
    System.out.println("Data file not found!");
    fnfe.printStackTrace();
    }catch(IOException ioe){
    System.out.println("IOException!");
    ioe.printStackTrace();
    }
    return tokenize(datasBuffer.toString());
    }

    private String[][] tokenize(String datas){
    datasTable = new String[height][width];
    StringTokenizer tokenizer = new StringTokenizer(datas, ";");
    for(int i=0 ; i<height ; i++){
    for(int j=0 ; j<width ; j++){
    String data = tokenizer.nextToken();
    datasTable[i][j] = data;
    }
    }
    return datasTable;
    }

    public int getWidth(){
    return width;
    }

    public int getHeight(){
    return height;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    TableFiller filler = new TableFiller();
    String dataFile = "C:/Temp/tableau.txt";
    String [][] myTable = filler.fill(dataFile);
    System.out.println("Tableau de données: " + myTable.toString());
    // Affichage du tableau
    for(int i=0 ; i<filler.getHeight() ; i++){
    System.out.println("Ligne "+(i+1)+": ");
    for(int j=0 ; j<filler.getWidth() ; j++){
    System.out.println(myTable[i][j]);
    }
    }
    }
    }

    ;-)

    HackTrack
    0
    1. foued
       
      merci c'est cool
      0
    2. foued
       
      merci c'est cool j'etais arriver en php mais la merci bcp :-))
      0