Scala - Read each character from a file Code Example

The code for Read each character from a file

import scala.io.Source

object MyClass {
    def main(args: Array[String]) {
        // file name
        val fileName = "file1.txt" 
        
        // creates iterable representation 
        // of the source file            
        val fs = Source.fromFile(fileName) 
        while (fs.hasNext)
        {
            println(fs.next)
        }
        
        // closing file
        fSource.close() 
    }
}

/*
Output:
Content of the file
*/
Code by IncludeHelp, on August 15, 2022 22:39

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.