Remplir un tableau java

cs_lila83 Messages postés 1 Date d'inscription mardi 3 avril 2007 Statut Membre Dernière intervention 25 novembre 2007 - 25 nov. 2007 à 10:35
saritasarita Messages postés 3 Date d'inscription vendredi 23 avril 2010 Statut Membre Dernière intervention 6 janvier 2012 - 25 oct. 2010 à 23:50
bonjour
besoin d'aide pour java.
je cherhce s'il y a une fonctio java qui lit direcetemnt un fichier *.txt et remplit un tableau à deux dimensions par ses lignes

merci de votre aide, c très urgent

2 réponses

Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
25 nov. 2007 à 12:22
Salut:

import java.io.*;
import java.util.*;

public class Test {
   
    // Séparateur entre deux mots successifs
    public static final String SPACE_SEPARATOR = " ";

    // Lit un fichier et remplit un matrice de mots
    public static String[][] readFile(String filename) {
        if (filename == null) {
            throw new NullPointerException();
        }

        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader(filename));
            List<String[]> list = new ArrayList<String[]>();

            String line = null;
            while((line = br.readLine()) != null) {
                list.add(line.split(SPACE_SEPARATOR));
            }
            br.close();

            String[][] matrix = new String[list.size()][];
            for (int i = 0; i < list.size(); ++i) {
                matrix[i] = list.get(i);
            }

            return matrix;
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // Affiche une matrice
    public static void showMatrix(String[][] matrix) {
        if (matrix == null) {
            throw new NullPointerException();
        }

        for (int i = 0; i < matrix.length; ++i) {
            for (int j = 0; j < matrix[i].length; ++j) {
                System.out.print(matrix[i][j] + "  ");
            }
            System.out.println();
        }
    }

    // Point d'entrée du programme
    public static void main(String[] args) {
        showMatrix(readFile("Test.java"));
    }

}
0
Rejoignez-nous