Rechercher : dans
Par :

[java] récupération de fichier texte

Dernière réponse le 27 mar 2007 à 15:34:54 flora806, le 23 mar 2007 à 14:11:13 
 Signaler ce message aux modérateurs

Salut à tous
j'espère que qqu pourrait m'aider :)
j'ai utilisé le code ci-dessous pour un récupérer un fichier texte en java. ce fichier contient des string + int

ce fichier se présente comme ça:
Id|age|sexe|profession|CodePostal

Mais en fait quand j'exécute ça, ça me donne rien en output et la démo ne marche pas.
je ne sais pas c dû à quoi :(
est-ce que qqu aurait une idée?

Merci.
Flora
********

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.Vector;

public class DemogData {
private String filePath;
private String [][] data;

public DemogData(String filePath) throws IOException {
super();
this.filePath = filePath;
parseFile();
}

private void parseFile() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));

String line = null;
List<String> items = new ArrayList<String>();
StringTokenizer splitter;
while ((line = reader.readLine()) != null) {
items.add(line);
}
data = new String [items.size()][5];
int counter = 0;
for (String item : items) {
splitter = new StringTokenizer(item, "|");
int nv = splitter.countTokens() ;

counter++;
}


}



public String[] getuList(String occupation) {
String[] uList = new String[0];
for (String[] dataLine : data) {
if (dataLine[3] == occupation) {
String[] newList = new String[uList.length + 3];
int counter = 0;
for (String entry : uList) {
newList[counter] = entry;
counter++;
}
newList[counter] = dataLine[0];
uList = newList;
}
}
return uList;
}

public static void main(String[] args) {
try {
DemogData demo = new DemogData("C:/u.txt");
String occupation = "student";
System.out.print("utilisateurs ayant profession" + " " + occupation + ": ");
for (String i : demo.getuList(occupation)){
System.out.println(i + " ");
}

} catch (IOException e) {
e.printStackTrace();
}
}

}

*************

Configuration: Windows XP
Internet Explorer 6.0

Meilleures réponses pour « [java] récupération de fichier texte » dans :
Servlets - Cookies VoirIntroduction aux cookies Les cookies représentent un moyen simple de stocker temporairement des informations chez un client, afin de les récupérer ultérieurement. Concrètement il s'agit de fichiers texte stockés sur le disque dur du client après...
Java - Premier programme VoirPremière application avec Java La première chose à faire est de créer un simple fichier texte (sans mise en forme) et de taper les quelques lignes suivantes : // Votre premiere application en Java class FirstApp { public static void main...
Java - Caractéristiques du langage VoirFichier source, compilation et machine virtuelle Le fichier source d'un programme écrit en Java est un simple fichier texte dont l'extension est par convention .java. Ce fichier source doit être un fichier texte non formatté, c'est-à-dire un...

1

 HackTrack, le 27 mar 2007 à 15:34:54
  • +3

Salut!

tiens, il me semble avoir déjà vu ce code quelquepart... ;-)

J'ai corrigé tes erreurs. Regarde les commentaires dans le code.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class DemogData {
	private String filePath;

	private String[][] data;

	public DemogData(String filePath) throws IOException {
		super();
		this.filePath = filePath;
		parseFile();
	}

	private void parseFile() throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filePath));

		String line = null;
		List<String> items = new ArrayList<String>();
		StringTokenizer splitter;
		while ((line = reader.readLine()) != null) {
			items.add(line);
		}
		data = new String[items.size()][5];
		int counter = 0;
		for (String item : items) {
			splitter = new StringTokenizer(item, "|");
			// La ligne suivante ne sert à rien, je l'ai mise en commentaire
			// int nv = splitter.countTokens();
			int columnCounter = 0;
			while (splitter.hasMoreElements()) {
				// Tu ne remplissais pas ton tableau: normal que ça ne
				// fonctionnait pas ;-)
				data[counter][columnCounter] = (String) splitter.nextElement();
				columnCounter++;
			}
			counter++;
		}
		System.out.println(data.length + " lines read from file");
	}

	public String[] getuList(String occupation) {
		ArrayList<String> uList = new ArrayList<String>();
		for (String[] dataLine : data) {
			// Lorsque tu veux comparer 2 String, utilise la méthode
			// String.equals() qui te premet de comparer les 2 String sur leur
			// contenu plutôt que sur leur référence
			// Essaye les deux lignes suivantes:
			// System.out.println(new String("a")==new String("a"));
			// System.out.println(new String("a").equals(new String("a")));
			if (dataLine[3].equals(occupation)) {
				uList.add(dataLine[0]);
			}
		}
		String[] uListArray = new String[uList.size()];
		return (String[]) uList.toArray(uListArray);
	}

	public static void main(String[] args) {
		try {
			DemogData demo = new DemogData("C:/u.txt");
			String occupation = "student";
			System.out.println("utilisateurs ayant profession" + " "
					+ occupation + ": ");
			for (String i : demo.getuList(occupation)) {
				System.out.println(i + " ");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}



;-)
HackTrack

Répondre à HackTrack