2014年02月26日 星期三 09:33
在Scala中,如果只是简单的读取文件内容,可以有两种方法,第一是使用Java的API,第二种就是使用scala.io.Source模块。
示例代码如下:
import scala.io.Source;
object S010_ReadFile {
def main(args: Array[String]): Unit = {
val filename="C:/Windows/System32/Drivers/etc/hosts";
for(line <- Source.fromFile(filename).getLines){
println(line);
}
val lines=Source.fromFile(filename).getLines.toList;
for(line <- lines){
println(line.reverse);
}
}
}
参考资料:
http://www.scala-lang.org/api/current/index.html#scala.io.Source
Zeuux © 2025
京ICP备05028076号