我试图打印出文件的内容,但是每次运行这段代码时,都不会返回任何内容。我知道文件存在(因为第4行),并且有内容。为什么这段代码不返回结果?
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)
})
}}发布于 2015-08-07 01:32:03
您的示例缺少对象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)
})
}}https://stackoverflow.com/questions/31867880
复制相似问题