快学Scala习题解答—第九章 文件和正则表达式

来源:互联网 发布:飞思卡尔单片机选型 编辑:程序博客网 时间:2024/05/22 06:35

9 文件和正则表达式 

9.1 编写一小段Scala代码,将某个文件中的行倒转顺序(将最后一行作为第一行,依此类推) 

Scala代码  收藏代码
  1. import io.Source  
  2. import java.io.PrintWriter  
  3.   
  4. val path = "test.txt"  
  5.   
  6. val reader = Source.fromFile(path).getLines()  
  7.   
  8. val result = reader.toArray.reverse  
  9.   
  10. val pw = new PrintWriter(path)  
  11.   
  12. result.foreach(line => pw.write(line + "\n"))  
  13.   
  14. pw.close()  


9.2 编写Scala程序,从一个带有制表符的文件读取内容,将每个制表符替换成一组空格,使得制表符隔开的n列仍然保持纵向对齐,并将结果写入同一个文件 
Scala代码  收藏代码
  1. import io.Source  
  2. import java.io.PrintWriter  
  3.   
  4. val path = "test.txt"  
  5.   
  6. val reader = Source.fromFile(path).getLines()  
  7.   
  8. val result = for ( t <- reader) yield t.replaceAll("\\t","    ")  
  9.   
  10. val pw = new PrintWriter(path)  
  11.   
  12. result.foreach(line => pw.write(line + "\n"))  
  13.   
  14. pw.close()  


9.3 编写一小段Scala代码,从一个文件读取内容并把所有字符数大于12的单词打印到控制台。如果你能用单行代码完成会有额外奖励 
Scala代码  收藏代码
  1. import io.Source  
  2.   
  3. Source.fromFile("test.txt").mkString.split("\\s+").foreach(arg => if(arg.length > 12) println(arg))  


9.4 编写Scala程序,从包含浮点数的文本文件读取内容,打印出文件中所有浮点数之和,平均值,最大值和最小值 
Scala代码  收藏代码
  1. import io.Source  
  2.   
  3. val nums = Source.fromFile("test.txt").mkString.split("\\s+")  
  4.   
  5. var total = 0d  
  6.   
  7. nums.foreach(total += _.toDouble)  
  8.   
  9. println(total)  
  10. println(total/nums.length)  
  11. println(nums.max)  
  12. println(nums.min)  


9.5 编写Scala程序,向文件中写入2的n次方及其倒数,指数n从0到20。对齐各列: 
  1         1 
  2         0.5 
  4         0.25 
...         ...
 
Scala代码  收藏代码
  1. import java.io.PrintWriter  
  2.   
  3. val pw = new PrintWriter("test.txt")  
  4.   
  5. for ( n <- 0 to 20){  
  6.   val t = BigDecimal(2).pow(n)  
  7.   pw.write(t.toString())  
  8.   pw.write("\t\t")  
  9.   pw.write((1/t).toString())  
  10.   pw.write("\n")  
  11. }  
  12.   
  13. pw.close()  


9.6 编写正则表达式,匹配Java或C++程序代码中类似"like this,maybe with \" or\\"这样的带引号的字符串。编写Scala程序将某个源文件中所有类似的字符串打印出来 
import io.Source 
Scala代码  收藏代码
  1. val source = Source.fromFile("test.txt").mkString  
  2.   
  3. val pattern = "\\w+\\s+\"".r  
  4.   
  5. pattern.findAllIn(source).foreach(println)  


9.7 编写Scala程序,从文本文件读取内容,并打印出所有的非浮点数的词法单位。要求使用正则表达式 
Scala代码  收藏代码
  1. import io.Source  
  2.   
  3. val source = Source.fromFile("test.txt").mkString  
  4.   
  5. val pattern = """[^((\d+\.){0,1}\d+)^\s+]+""".r  
  6.   
  7. pattern.findAllIn(source).foreach(println)  


9.8 编写Scala程序打印出某个网页中所有img标签的src属性。使用正则表达式和分组 
Scala代码  收藏代码
  1. import io.Source  
  2.   
  3. val source = Source.fromFile("D:\\ProgramCodes\\ScalaTest\\src\\test.txt").mkString  
  4. val pattern = """<img[^>]+(src\s*=\s*"[^>^"]+")[^>]*>""".r  
  5.   
  6. for (pattern(str) <- pattern.findAllIn(source)) println(str)  


9.9 编写Scala程序,盘点给定目录及其子目录中总共有多少以.class为扩展名的文件 
Scala代码  收藏代码
  1. import java.io.File  
  2.   
  3. val path = "."  
  4.   
  5. val dir = new File(path)  
  6.   
  7.   
  8. def subdirs(dir:File):Iterator[File]={  
  9.   val children = dir.listFiles().filter(_.getName.endsWith("class"))  
  10.   children.toIterator ++ dir.listFiles().filter(_.isDirectory).toIterator.flatMap(subdirs _)  
  11. }  
  12.   
  13. val n = subdirs(dir).length  
  14.   
  15. println(n)  


9.10 扩展那个可序列化的Person类,让它能以一个集合保存某个人的朋友信息。构造出一些Person对象,让他们中的一些人成为朋友,然后将Array[Person]保存到文件。将这个数组从文件中重新读出来,校验朋友关系是否完好 
注意,请在main中执行。脚本执行无法序列化。
 
Scala代码  收藏代码
  1. import collection.mutable.ArrayBuffer  
  2. import java.io.{ObjectInputStream, FileOutputStream, FileInputStream, ObjectOutputStream}  
  3.   
  4. class Person(var name:String) extends Serializable{  
  5.   
  6.   val friends = new ArrayBuffer[Person]()  
  7.   
  8.   def addFriend(friend : Person){  
  9.     friends += friend  
  10.   }  
  11.   
  12.   override def toString() = {  
  13.     var str = "My name is " + name + " and my friends name is "  
  14.     friends.foreach(str += _.name + ",")  
  15.     str  
  16.   }  
  17. }  
  18.   
  19.   
  20. object Test extends App{  
  21.   val p1 = new Person("Ivan")  
  22.   val p2 = new Person("F2")  
  23.   val p3 = new Person("F3")  
  24.   
  25.   p1.addFriend(p2)  
  26.   p1.addFriend(p3)  
  27.   println(p1)  
  28.   
  29.   val out = new ObjectOutputStream(new FileOutputStream("test.txt"))  
  30.   out.writeObject(p1)  
  31.   out.close()  
  32.   
  33.   val in =  new ObjectInputStream(new FileInputStream("test.txt"))  
  34.   val p = in.readObject().asInstanceOf[Person]  
  35.   println(p)  
  36. }  
0 0
原创粉丝点击