Thomas Darimont
Erfahrenes Mitglied
Hallo!
Gruß Tom
Java:
/**
*
*/
package de.tutorials;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;
/**
* @author Tom
*
*/
public class TextFileReaderExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
TextFile textFile = new TextFile(new File("c:/doubleNumbers.txt"));
for (String line : textFile) {
System.out.println(line);
}
textFile.close();
}
static class TextFile implements Iterable<String> {
Scanner scanner;
public TextFile(File file) {
try {
this.scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Iterator<String> iterator() {
return new Iterator<String>() {
public boolean hasNext() {
return scanner.hasNextLine();
}
public String next() {
return scanner.nextLine();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public void close() {
scanner.close();
}
}
}
Gruß Tom