scala文件和目录操作

http://blog.csdn.net/caiandyong/article/details/52005580

scala文件

 

1.读取行

要读取文件的所有行,可以调用scala.io.Source对象的getLines方法:

[java] view plain copy
 
  1. import scala.io.Source  
  2.   
  3. object HelloWord{  
  4.   def main(args:Array[String]):Unit = {  
  5.     val fileName = "d:\\scalaTestFile.txt"  
  6.     val source = Source.fromFile(fileName)  
  7.     val lines = source.getLines  
  8.     source.close;//记得要关闭source  
  9.   
  10.     for(line <- lines){  
  11.       println(line)  
  12.     }  
  13.   }  
  14. }  



也可以对getLines应用toArray或toBuffer方法,将这些行放到数组或缓冲当中。

[java] view plain copy
 
  1. val lines1 = source.getLines.toArray  
  2. val lines2 = source.getLines.toBuffer  



将文件内容读成一个字符串:
val lines = source.mkString


2.读取字符

要从文件中读取字符,可以直接把Source对象当做迭代器:

[java] view plain copy
 
  1. val fileName = "d:\\scalaTestFile.txt"  
  2.     val source = Source.fromFile(fileName)  
  3.      
  4.     for(c <- source){  
  5.       println(c)  
  6.     }  



如果想查看某个字符,但是不处理掉的话,调用source对象的buffered方法。

[java] view plain copy
 
  1. val fileName = "d:\\scalaTestFile.txt"  
  2. val source = Source.fromFile(fileName)  
  3. val iter = source.buffered  
  4.   
  5. while(iter.hasNext){  
  6.   if(iter.next == '王'){  
  7.     println("wang")  
  8.   }else{  
  9.     println("-")  
  10.   }  
  11. }  



3.读取词法单元或数字

通过split方法对转化成行的文件内容进行划分,通过toInt或toDouble方法把字符转化成整数或浮点数。

[java] view plain copy
 
  1. val fileName = "d:\\scalaTestFile.txt"  
  2. val source = Source.fromFile(fileName)  
  3. val iter = source.mkString.split("\\s+")  
  4.   
  5. println(iter(0))  
  6.   
  7. val num = for(w <- iter) yield w.toDouble  
  8.   
  9. for(i <- num) println(i)  



4.从URL或其它资源读取

[java] view plain copy
 
  1. import scala.io.Source  
  2. import scala.util.control.Breaks._  
  3.   
  4. object HelloWord{  
  5.   def main(args:Array[String]):Unit = {  
  6.     val source1 = Source.fromURL("http://baidu.com")//URL读取  
  7.     val source2 = Source.fromString("hello")//读取给定的字符串-多用于调试  
  8.     val source3 = Source.stdin//从标准输入读取  
  9.       
  10.     breakable{  
  11.         while(source3.hasNext){  
  12.           val s3in = source3.next  
  13.           if(s3in == 'q'){  
  14.             break  
  15.           }else{  
  16.             println(s3in)  
  17.           }  
  18.         }  
  19.     }  
  20.   }  
  21. }  




5.写入文本

Scala没有內建的对写入文件的支持,要写入文本文件,可以使用java.io.PrintWriter.

[java] view plain copy
 
  1. import java.io.PrintWriter  
  2.   
  3. object HelloWord{  
  4.   def main(args:Array[String]):Unit = {  
  5.       val out = new PrintWriter("d:\\testScalaWrite.txt")  
  6.       for(i <- 1 to 10){  
  7.         out.print(i + "+")  
  8.       }  
  9.       out.close()  
  10.   }  
  11. }  



6.访问目录

[java] view plain copy
 
    1. //遍历某目录下所有的子目录  
    2. import java.io.PrintWriter  
    3. import java.io.File  
    4. import scala.reflect.io.Directory  
    5.   
    6. object HelloWord{  
    7.   def main(args:Array[String]):Unit = {  
    8.       for(d <- subDir(new File("d:\\AAA\\")))  
    9.         println(d)  
    10.   }  
    11.    
    12.   def subDir(dir:File):Iterator[File] ={  
    13.     val children = dir.listFiles().filter(_.isDirectory())  
    14.     children.toIterator ++ children.toIterator.flatMap(subDir _)  
    15.   }  
    16. }  
    17.   
    18. //遍历某目录下所有的文件和子文件  
    19.   def main(args:Array[String]):Unit = {  
    20.       for(d <- subDir(new File("d:\\AAA\\")))  
    21.         println(d)  
    22.   }  
    23.    
    24.   def subDir(dir:File):Iterator[File] ={  
    25.     val dirs = dir.listFiles().filter(_.isDirectory())  
    26.     val files = dir.listFiles().filter(_.isFile())  
    27.     files.toIterator ++ dirs.toIterator.flatMap(subDir _)  
    28.   }  

posted on 2017-07-25 10:13  小西红柿  阅读(340)  评论(0)    收藏  举报

导航