TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to F#

F# Files: open System.IO, use StreamReader

Use System.IO and StreamReader to read lines of text files. Specify the use, and open, keywords.
Files. In files we find persistent data. Files introduce complexity and bugs. A file may be unavailable. It may have invalid formatting. It may be corrupted.
Open System.IO. In the System.IO namespace we find many useful types like StreamReader. Methods like File.ReadAllLines are also included.
StreamReader. Let us begin with a StreamReader example. We introduce a type called Data. In the Read function, a member of Data, we open a StreamReader and read in a file's lines.

Use: This keyword ensures the StreamReader is correctly disposed of when it is no longer needed.

While: We use a while-loop to continue iterating over the lines in the file while they can be read. We read all the lines and print them.

For
F# program that uses StreamReader, System.IO open System.IO type Data() = member x.Read() = // Read in a file with StreamReader. use stream = new StreamReader @"C:\programs\file.txt" // Continue reading while valid lines. let mutable valid = true while (valid) do let line = stream.ReadLine() if (line = null) then valid <- false else // Display line. printfn "%A" line // Create instance of Data and Read in the file. let data = Data() data.Read() Contents of file.txt: carrot squash yam onion tomato Output "carrot" "squash" "yam" "onion" "tomato"
File.ReadAllLines. This is a simpler way to read all the lines in a file, but it may be less efficient on large files. ReadAllLines returns an array of strings.

Here: We use Seq.toList to get a list from the array. And with Seq.where we get a sequence of only capitalized lines in the list.

F# program that uses File.ReadAllLines, Seq.toList open System open System.IO let lines = File.ReadAllLines(@"C:\programs\file.txt") // Convert file lines into a list. let list = Seq.toList lines printfn "%A" list // Get sequence of only capitalized lines in list. let uppercase = Seq.where (fun (n : String) -> Char.IsUpper(n, 0)) list printfn "%A" uppercase Contents of file.txt: ABC abc Cat Dog bird fish Output ["ABC"; "abc"; "Cat"; "Dog"; "bird"; "fish"] seq ["ABC"; "Cat"; "Dog"]
A summary. In F# we have access to the .NET Framework's IO library. This enables efficient and well-tested use of files. With StreamReader we iterate over the lines in a file.
Although we can use constructs like the while-loop, if and else, a simpler approach is to use methods like File.ReadAllLines. The returned array can be used easily in F#.Array
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf