[java] récupération de fichier texte
Résolu
flora806
-
Alex -
Alex -
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();
}
}
}
*************
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();
}
}
}
*************
A voir également:
- [java] récupération de fichier texte
- Fichier bin - Guide
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Fichier epub - Guide
- Fichier rar - Guide
1 réponse
Salut!
tiens, il me semble avoir déjà vu ce code quelquepart... ;-)
J'ai corrigé tes erreurs. Regarde les commentaires dans le code.
;-)
HackTrack
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
Une idée??? merci d'avance......