Une classe a fait son apparition dans le package java.util dans la version 1.5.0 de java Cette classe s'interface parfaitement avec des flux de lecture pour y permettre une lecture puissante et pratique ! A simple text scanner which can parse primitive types and strings using regular expressions. A For example, this code allows a user to read a number from System.in: Scanner sc = new Scanner(System.in); As another example, this code allows Scanner sc = new Scanner(new File("myNumbers")); The scanner can also use delimiters other than whitespace. This example reads several items in from a string: String input = "1 fish 2 fish red fish blue fish"; prints the following output: 1 The same output can be generated with this code, which uses a regular expression to parse all four tokens at once: String input = "1 fish 2 fish red fish blue fish"; The default whitespace delimiter used by a scanner is as
recognized by A scanning operation may block waiting for input. The The When a scanner throws an Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern "\\s+" will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern "\\s" could return empty tokens since it only passes one space at a time. A scanner can read text from any object which implements the When a A Unless otherwise mentioned, passing a useRadix(int) method.
Utilisation
Regardons les constructeurs : Scanner(File source) Scanner(File source, String charsetName) Scanner(InputStream source) Scanner(InputStream source, String charsetName) Scanner(Readable source) Scanner(ReadableByteChannel source) Scanner(ReadableByteChannel source, String charsetName) Scanner(String source)
On se rend vite compte que cette classe peu lire dans n?importe quoi ! Tentons une expérience.
import java.util.Scanner;
public class Essai { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = ""; while(s.compareTo("ok")!=0){ s = sc.nextLine(); System.out.println(s); }
} } Miracle ca marche !!! Simple non ? Maintenant je ne sais pas vous mais un constructeur qui m?as beaucoup étonné est Scanner(String)
Essayons quelque chose avec les méthodes disponibles :
import java.util.Scanner;
public class Essai { public static void main(String[] args) { String s = "Thomas Sanchez 16"; Scanner sc = new Scanner(s); System.out.println("prenom : " + sc.next() + "\nnom : " + sc.next() + "\nage : " + sc.nextInt()); } } Cool non ? Bon cette classe a l?air bien mais fait-elle des merveilles pour lire un fichier aussi ? Essayons :
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class Essai { public static void main(String[] args) {
Scanner sc; try { sc = new Scanner(new File("essai.txt")); while(sc.hasNext()) System.out.println(sc.nextLine()); } catch (FileNotFoundException e) { e.printStackTrace(); }
} } Et hop voila le fichier lu !! Bon
ca pourrais durer longtemps pour que je vous montre toute les
utilisations possible....a vous de fouiller plus en detail! |