Reading text from a file

From CodeCodex

Revision as of 19:37, 31 July 2006 by 206.160.140.2 (Talk)

Implementations

Java

    try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
            process(str);
        }
        in.close();
    } catch (IOException e) {
    }

OCaml

# let process_file process filename =
    let ch = open_in filename in
    try while true do process(input_line ch) done
    with _ -> close_in ch;;
val process_file : (string -> 'a) -> string -> unit = <fun>