不并行打印文件内容
not printing file contents in parallel
我试图打印出文件的内容,但每次我 运行 这段代码什么都没有 return。我知道文件存在(因为第 4 行)并且有内容。为什么此代码 return 没有结果?
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)
})
编辑:提出答案后,我更新了我的代码以采纳建议:
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)
})
}}
您的示例缺少对象 HelloWorld 的右大括号。
此外,您需要将 val 放入对象中。如果您尝试 scalac
您的文件,它应该会报错。工作代码:
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)
})
}}
编辑代码的正确版本:
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)
})
}}
我试图打印出文件的内容,但每次我 运行 这段代码什么都没有 return。我知道文件存在(因为第 4 行)并且有内容。为什么此代码 return 没有结果?
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)
})
编辑:提出答案后,我更新了我的代码以采纳建议:
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)
})
}}
您的示例缺少对象 HelloWorld 的右大括号。
此外,您需要将 val 放入对象中。如果您尝试 scalac
您的文件,它应该会报错。工作代码:
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)
})
}}
编辑代码的正确版本:
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)
})
}}