Salut à tous,
je viens de découvrir la classe Scanner et j'essaie de l'utiliser pour lire un fichier .CSV.
voici une ligne du fichier en entrée :
et voici le code de ma classe :1,2545,1152,231,17/03/2000 04:00,G,0,-78.391,-9.454,1, 0.1,270,AGUILA REAL ,AGUILAREAL, 1, 4 ,4639.577,567.24,NA,NA,NA,NA
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public class LireFichier { public LireFichier(String nomFichier) throws IOException{ Scanner sc; PrintWriter pw = new PrintWriter(new FileWriter("test.txt")); try { sc = new Scanner(new File(nomFichier)).useDelimiter(","); boolean eof = false; while (!eof) { //row names System.out.println(sc.next()); System.out.println(sc.next()); System.out.println(sc.next()); //numero emb System.out.println(sc.next()); //Datacion pw.print(sc.next()); pw.print(","); //clase System.out.println(sc.next()); //tipo rec System.out.println(sc.next()); //X pw.write(sc.next()); pw.print(","); //Y pw.print(sc.next()); pw.print(","); //NUMERO.ZON System.out.println(sc.next()); //VELOCIDAD pw.print(sc.next()); pw.print(","); //RUMBO pw.print(sc.next()); pw.print(","); //EMBARCACIO System.out.println(sc.next()); //BoatNAbb System.out.println(sc.next()); //ref.voyage System.out.println(sc.next()); //hora pw.print(sc.next()); pw.print(","); //Longmn System.out.println(sc.next()); //Latmn System.out.println(sc.next()); //duracion System.out.println(sc.next()); //cambio.vel String tmp = sc.next(); pw.print(tmp); System.out.println("cambio.vel"+tmp); pw.print(","); //distancia String tmp2 = sc.next(); pw.print(tmp2); System.out.println("distancia"+tmp2); pw.print(","); //cambio.rumbo String tmp3 = sc.next(); pw.print(tmp3); System.out.println(tmp3); //pw.print("\n"); } sc.close(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
à l'exécution, j'obtiens le message d'erreur suivant :
[QUOTE]
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at miseenformefichiersmatthieu.LireFichier.<init>(LireFichier.java:29)
at miseenformefichiersmatthieu.Main.<init>(Main.java:17)
at miseenformefichiersmatthieu.Main.main(Main.java:20)
Java Result: 1[
/QUOTE]
Globalement, je cherche à extraire que certaines colonnes du fichier d'entrée et de les reconduire dans un fichier de sortie...peut-être que d'autres classes que Scanner sont plus adaptées à mon pb ?
Merci d'avance
Partager