Leggere un file con java.util.Scanner

Leggere un file con java.util.Scanner
Scritto da Administrator
Giovedì 20 Gennaio 2011 14:14 -
Java - Leggere un file con java.util.Scanner
Il metodo seguente mostra come leggere un file con java.util.Scanner.
{codecitation style="brush: java;"} package it.soluzionijava.util; import java.io.File; import
java.io.FileNotFoundException; import java.util.Scanner; public class ScannerReadFile {
public static void main(String[] args) { // // Create an instance of File for data.txt file. //
File file = new File("data.txt");
try { // // Create a new Scanner object which will read the
data // from the file passed in. To check if there are more // line to read from it we check by
calling the // scanner.hasNextLine() method. We then read line one // by one till all line is
read. // Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) {
String
line = scanner.nextLine();
System.out.println(line); } } catch (FileNotFoundException e) {
e.printStackTrace(); } } } {/codecitation}
1/1