A voir également:
- Fichier text et java
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Comment ouvrir un fichier epub ? - Guide
1 réponse
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
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
16 févr. 2002 à 18:50
16 févr. 2002 à 18:53