使用 Scala 2.10.2------------
我是一名Java工程师,从昨天开始学习Scala,但是现在卡住了,这个简单的代码对我不起作用,但是当我使用java编写它时,它可以正常工作:
package lesson4
import scala.io.Source
import scala.reflect.io.File
object Test {
def main(args: Array[String]): Unit = {
var filePath = Source.getClass().getResource("/lesson4/test.txt")
var file = Source.fromFile(filePath.getFile())
var lines = file.getLines
lines.foreach(println)
}
}
该文件位于正确的路径中: 但代码不起作用:
Exception in thread "main" java.io.FileNotFoundException: /Users/wenjiezhang/Desktop/source_files/git_hub%20workspace/Learning%20Scala/ScalaLearning/bin/lesson4/test.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at scala.io.Source$.fromFile(Source.scala:90)
at scala.io.Source$.fromFile(Source.scala:75)
at scala.io.Source$.fromFile(Source.scala:53)
at lesson4.Test$.main(Test.scala:20)
at lesson4.Test.main(Test.scala)