Search code examples
scalafile-iofileinputstream

not printing file contents in parallel


I am trying to print out the contents of the file, yet every time I run this code nothing returns. I know the file exists (because of line 4) and has contents. Why doesnt this code return results?

 import scala.xml._
 import collection.mutable.HashMap
 import java.nio.file.{Paths, Files}

 val noDupFile="nodup_steam_out.txt"
 println(Files.exists(Paths.get(noDupFile))) //returns true

object HelloWorld {
    def main(args: Array[String]) {

    io.Source.fromFile(noDupFile).getLines().toStream.par.foreach((res:String)=>{
        println(res)
    })

EDIT: After proposed answers I updated my code to take suggestions:

 import scala.xml._
 import collection.mutable.HashMap
 import java.nio.file.{Paths, Files}

 val api="BLAH"

object HelloWorld {
    def main(args: Array[String]) {
    val noDupFile="nodup_steam_out.txt"
    println(Files.exists(Paths.get(noDupFile))) //returns true
    io.Source.fromFile(noDupFile).getLines().toStream.par.foreach((res:String)=>{
        /*
        val url=("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key="+api+"&steamids="+res+"&format=xml")
        //http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamid=76561197960435530&relationship=friend
        //http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=XXXXXXXXXXXXXXXXX&steamid=76561197960434622&format=json
        //http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=XXXXXXXXXXXXXXXXX&steamid=76561197960434622&format=json
        //http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=XXXXXXXXXXXXXXXXX&steamids=XXXXXXXX,YYYYY
        val str = scala.io.Source.fromURL(url.toString,"utf-8").mkString
        val x=xml.XML.loadString(str)       
         val allNodes = x \\ "response" \\ "players" \\ "player" flatMap(_.child) filter(!_.isAtom)
        allNodes.foreach(n => {
            print(s"${n.label}: ${n.text},")
        })
        */
        println(res)
    })
}}

Solution

  • Your example is missing closing braces for the Object HelloWorld.

    Also, you need to place your val into the object. If you try to scalac your file it should spit an error. Working code:

    import scala.xml._
    import collection.mutable.HashMap
    import java.nio.file.{Paths, Files}
    
    object HelloWorld {
    
    val noDupFile="nodup_steam_out.txt"
    println(Files.exists(Paths.get(noDupFile))) //returns true
        def main(args: Array[String]) {
    
        io.Source.fromFile(noDupFile).getLines().toStream.par.foreach((res:String)=>{
            println(res)
        })  
    }}
    

    Correct version of the edit code:

    import scala.xml._
     import collection.mutable.HashMap
     import java.nio.file.{Paths, Files}
    
        object HelloWorld {
            def main(args: Array[String]) {
                val api="BLAH"
                val noDupFile="nodup_steam_out.txt"
                println(Files.exists(Paths.get(noDupFile))) //returns true
                io.Source.fromFile(noDupFile).getLines().toStream.par.foreach((res:String)=>{
                    println(res)
                })
        }}