Java
public class TextFileReader {
public interface ScannerOperation {
void handleNext(Scanner sc);
}
/**
* @param path The path of the file as a string.
* @param f Lambda function to apply. Should advance to the next token in
* some way.
* @throws IOException
*/
public static void readTextFile(String path, ScannerOperation f) throws IOException {
File inputFile = new File(path);
try (Scanner sc = new Scanner(inputFile)) {
while (sc.hasNext()) {
f.handleNext(sc);
}
}
}
}
public class ExampleImplementation {
public static void main(String[] args) {
try {
TextFileReader.readTextFile("file.txt", (sc) -> {
System.out.println(sc.next());
});
} catch (IOException e) {
System.err.println(e.toString());
return;
}
}
}