Scala進階之路-I/O流操作之文件處理
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
說起Scala語言操作文件對象其實是很簡單的,大部分代碼和Java相同。
一.使用Scala拷貝文件實現
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Scala%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.file 7 8 import java.io._ 9 10 object FileDemo { 11 /** 12 * 定義讀取文件的方法 13 */ 14 def readFile(filePath:String): Unit ={ 15 val f = new File(filePath) 16 val fis = new FileInputStream(f) 17 val buf = new BufferedReader(new InputStreamReader(fis)) 18 var line = ""; 19 /** 20 * 我這里定義一個標志位,判斷文件是否讀取完畢,不建議使用break,不僅僅是因為它需要導報,而是因為它是以拋異常的 21 * 方式結束了整個程序!這一點我真的想吐槽Scala啦!比起Python,Java,Golang,Scala是沒有continue和break關鍵字的! 22 */ 23 var flag:Boolean= true 24 while (flag){ 25 line = buf.readLine() 26 //注意,Scala在讀取文件時,如果讀到最后會返回一個null值,因此,此時我們將標志位改為false,以便下一次結束while循環 27 if (line == null){ 28 flag = false 29 }else{ 30 println(line) 31 } 32 } 33 buf.close() 34 fis.close() 35 } 36 37 /** 38 * 拷貝文本文件 39 */ 40 def copyFile(Input:String)(OutPut:String): Unit ={ 41 val input = new File(Input) 42 val output = new File(OutPut) 43 val fis = new FileInputStream(input) 44 val fos = new FileOutputStream(output) 45 val buf = new BufferedReader(new InputStreamReader(fis)) 46 val cache = new BufferedWriter(new OutputStreamWriter(fos)) 47 var line = ""; 48 var flag:Boolean= true 49 while (flag){ 50 line = buf.readLine() 51 if (line == null){ 52 flag = false 53 }else{ 54 cache.write(line) 55 cache.write("\r\n") 56 cache.flush() 57 } 58 } 59 cache.close() 60 fos.close() 61 fis.close() 62 println("拷貝完畢") 63 } 64 65 66 /** 67 * 拷貝任意類型文件,包括二進制文件 68 */ 69 def copyFile2(Input:String)(OutPut:String): Unit ={ 70 val fis = new FileInputStream(Input) 71 val fos = new FileOutputStream(OutPut) 72 //定義緩沖區 73 val buf = new Array[Byte](1024) 74 var len = 0 75 76 /** 77 * 注意:len = fis.read(buf)是一個賦值操作,這個賦值操作是沒有任何的返回值的喲!因此我們需要返回len的值。 78 */ 79 while ({len = fis.read(buf);len} != -1){ 80 fos.write(buf,0,len) 81 } 82 fos.close() 83 fis.close() 84 println("拷貝完畢") 85 } 86 87 def main(args: Array[String]): Unit = { 88 var input = "D:\\BigData\\JavaSE\\yinzhengjieData\\1.java" 89 var output = "D:\\BigData\\JavaSE\\yinzhengjieData\\2.java" 90 // readFile(input) 91 copyFile2(input)(output) 92 } 93 } 94 95 96 97 98 /* 99 以上代碼輸出結果如下 : 100 拷貝完畢 101 */
二.讀取用戶的輸出
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Scala%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.file 7 8 import scala.io.StdIn 9 10 object ConsoleDemo { 11 def main(args: Array[String]): Unit = { 12 //控制台交互--老API 13 print("請輸入內容:") 14 val consoleLine1 = Console.readLine() 15 println("老API輸入的內容是:" + consoleLine1) 16 17 //控制台交互--新API 18 print("請輸入內容(新API):") 19 val consoleLine2 = StdIn.readLine() 20 println("新API輸入的內容是:" + consoleLine2) 21 } 22 }
以上代碼執行結果如下:
三.Scala文件處理常用方法
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Scala%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.file 7 8 object SourceDemo { 9 def main(args: Array[String]): Unit = { 10 var input = "D:\\BigData\\JavaSE\\yinzhengjieData\\1.java" 11 val f = scala.io.Source.fromFile(input) 12 println(f) 13 14 /** 15 * 迭代打印文件中的每行內容 16 */ 17 // val it = f.getLines() 18 // for(x <- it){ 19 // println(x) 20 // } 21 22 /** 23 * 讀取整個文件串 24 */ 25 println(f.mkString) 26 27 /** 28 * 迭代每個字符 29 */ 30 // for(c <- f){ 31 // print(c) 32 // } 33 34 /** 35 * 使用正則 s:空白符 S:非空白符 36 * 所謂的空白符就是指:空格,制表符,換行符等等 37 */ 38 // val arr = f.mkString.split("\\s+") 39 // for(a <- arr){ 40 // println(a) 41 // } 42 43 } 44 }
使用Scala爬取網頁,在網上找了一些寫的都長篇大論,屬實懶得看,爬蟲的話建議還是用Java或者Python去寫,Java爬取網頁的筆記可參考:https://www.cnblogs.com/yinzhengjie/p/9366013.html。
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Scala%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.file 7 8 import java.io.FileOutputStream 9 10 object ReptilianDemo { 11 12 def copyFile(Input:String)(OutPut:String): Unit ={ 13 val fos = new FileOutputStream(OutPut) 14 fos.write(Input.getBytes()) 15 println("拷貝完畢") 16 } 17 18 def main(args: Array[String]): Unit = { 19 /** 20 * Scala的fromURL方法我是不推薦使用的,因為爬去的內容不完全,需要設置相應的參數,建議用java代碼或者Python去爬取 21 * 可參考:https://www.cnblogs.com/yinzhengjie/p/9366013.html 22 */ 23 val res = scala.io.Source.fromURL("https://www.cnblogs.com/yinzhengjie") 24 val html = res.mkString 25 println(html) 26 var output = "D:\\BigData\\JavaSE\\yinzhengjieData\\尹正傑.html" 27 copyFile(html)(output) 28 29 } 30 }
訪問成功的話,在控制端會打印如下信息: