Read Two Lines, .NET-style

Category:
Input/Output
Description: Line-directed text file input using .NET Stream/StreamReader/StreamWriter abstractions.
Code:
let LineDirectedInputSample2() = 

// Write a test file
System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file.";
"It is easy to read." |]);

// Now read it. We use to ensure the file is closed even if an exception occurs
// during reading.
let line1,line2 =
use sr = System.IO.File.OpenText @"test.txt"
let line1 = sr.ReadLine()
let line2 = sr.ReadLine()
(line1,line2)
printfn "line1=%s\nline2=%s" line1 line2

Execution Result:
line1=This is a test file.
line2=It is easy to read.

Read Two Lines, .NET-style

Category:
Input/Output
Description: Line-directed text file input using .NET Stream/StreamReader/StreamWriter abstractions.
Code:
let LineDirectedInputSample3() = 

// Write a test file
System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file.";
"It is easy to read." |]);
// Now read it
let lines = System.IO.File.ReadAllLines @"test.txt"
printfn "%s" (sprintf "%A" lines)

Execution Result:
[|"This is a test file."; "It is easy to read."|]

Read Entire File, .NET-style

Category:
Input/Output
Description: Read an entire text file as a string using .NET I/O utilities and abstractions.
Code:
let EntireFileInputSample2() = 

// Write a test file
System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file.";
"It is easy to read." |]);
// Now read it
let res = System.IO.File.ReadAllText(@"test.txt")
printfn "%s" res


Execution Result:
This is a test file.
It is easy to read.


Read Entire CSV File, .NET-style

Category:
Input/Output
Description: Read an entire text file as a string using .NET I/O utilities and abstractions.
Code:
let ReadCSVFile1() = 

// Write a test file
System.IO.File.WriteAllLines(@"test.csv", [| "Desmond, Barrow, Market Place, 2";
"Molly, Singer, Band, 12" |]);
// Now read it
let linesSplitIntoWords =
System.IO.File.ReadAllLines(@"test.csv")
|> Array.map (fun line -> line.Split [|',';' ';'\t'|])
printfn "%A" linesSplitIntoWords
Execution Result:
[|[|"Desmond"; ""; "Barrow"; ""; "Market"; "Place"; ""; "2"|];
[|"Molly"; ""; "Singer"; ""; "Band"; ""; "12"|]|]

Lazily Enumerate CSV File

Category:
Input/Output
Description: Build an IEnumerable for on-demand reading of a CSV file using .NET I/O utilities and abstractions
Code:
let EnumerateCSVFile1() = 

// Write a test file
System.IO.File.WriteAllLines(@"test.csv", [| "Desmond, Barrow, Market Place, 2";
"Molly, Singer, Band, 12" |]);

/// This function builds an IEnumerable object that enumerates the CSV-split
/// lines of the given file on-demand
let CSVFileEnumerator(fileName) =

// The function is implemented using a sequence expression
seq { use sr = System.IO.File.OpenText(fileName)
while not sr.EndOfStream do
let line = sr.ReadLine()
let words = line.Split [|',';' ';'\t'|]
yield words }

// Now test this out on our test file, iterating the entire file
let test = CSVFileEnumerator(@"test.csv")
printfn "-------Enumeration 1------";
test |> Seq.iter (string >> printfn "line %s");
// Now do it again, this time determining the numer of entries on each line.
// Note how the file is read from the start again, since each enumeration is
// independent.
printfn "-------Enumeration 2------";
test |> Seq.iter (Array.length >> printfn "line has %d entries");
// Now do it again, this time determining the numer of entries on each line.
// Note how the file is read from the start again, since each enumeration is
// independent.
printfn "-------Enumeration 3------";
test |> Seq.iter (Array.map (fun s -> s.Length) >> string >> printfn "lengths of entries: %s")

Execution Result:
-------Enumeration 1------
line System.String[]
line System.String[]
-------Enumeration 2------
line has 8 entries
line has 7 entries
-------Enumeration 3------
lengths of entries: System.Int32[]
lengths of entries: System.Int32[]

Last edited Sep 12, 2011 at 11:36 PM by ttliu2000, version 1

Comments

No comments yet.