目錄
在本篇文章中你將會(huì)學(xué)習(xí)并了解常用的文件處理任務(wù),例如讀取文件的一行文本,本博客的要點(diǎn)包含:
1.Source.fromFile(...).getLines.toArray 輸出文件所有行
2.Source.fromFile(...).mkString 以字符串形式輸出文件內(nèi)容
3.將字符串轉(zhuǎn)換為數(shù)字,可以使用toInt或toDouble方法
4.使用java的PrintWriter寫(xiě)入文本文件
5.“正則”.r是一個(gè)Regex對(duì)象
6.若你的正則表達(dá)式包含反斜杠或者引號(hào),請(qǐng)用"""..."""
7.正則模式包含分組,可以使用for(regex(變量1...,變量2)- 字符串)
本篇文章要點(diǎn)如下:
讀取行
// 讀取文件所有的行,可以調(diào)用scala.io.Source對(duì)象的getLines方法: val source = Source.fromFile("a.txt","utf-8") val lineIterator = source.getLines
結(jié)果是迭代器可以使用for循環(huán)處理這些行
for(i - lineIterator) println(i)
也可以使用迭代器應(yīng)用toArray或toBuffer方法,將這些行放到數(shù)組力或者數(shù)組緩沖行中,若想將讀取的的文件作為一個(gè)字符串,只需val conents = source.mkString
下面是簡(jiǎn)單的代碼實(shí)例:讀取桌面上的a.txt
object ReadFile { def main(args: Array[String]): Unit = { val read = new ReadFile() val resource: String = "C:\\Users\\DonnieGao\\Desktop\\a.txt" val encode = "UTF-8" read.readFile(resource, encode) println(read.readFileToStr(resource, encode)) } } class ReadFile { /** * 一行行讀取文件的內(nèi)容 * * @param resource 文件路徑 * @param code 文件編碼格式 */ def readFile(resource: String, code: String): Unit = { var source: BufferedSource = null try { // 獲取文件的Source對(duì)象,第一個(gè)參數(shù)是文件的路徑,第二個(gè)文件的編碼格式 source = Source.fromFile(resource, code) val lineIterator = source.getLines() while (true) { if (lineIterator.hasNext) { println(lineIterator.next()) } else { return } } } finally { // 釋放資源 source.close() } } /** * 將文本文件所有內(nèi)容作為字符串 * * @param resource 文件路徑 * @param code 文件編碼格式 * @return */ def readFileToStr(resource: String, code: String): String = { // 獲取文件的Source對(duì)象,第一個(gè)參數(shù)是文件的路徑,第二個(gè)文件的編碼格式 var source: BufferedSource = null try { source = Source.fromFile(resource, code) source.mkString } finally { source.close() } } }
讀取字符
要將文件中讀取單個(gè)字符,可以把Source對(duì)象當(dāng)作迭代器,若僅僅只是想查看字符可以調(diào)用Source對(duì)象的buffered方法。
讀取詞法單元和數(shù)字
讀取源文件中所有空格隔開(kāi)的詞法單元
val tokens = source.mkString.split("\\s+")
若有個(gè)基本都是浮點(diǎn)型的文件,可以將其讀取到數(shù)組中:
val numbers = for (w - tokens) yield w.toDouble 或者也可 val numbers = token.map(_.toDouble)
讀取二進(jìn)制文件
Scala并沒(méi)有提供讀取二進(jìn)制文件的方法,可以使用java讀取二進(jìn)制的方法,代碼示例
val file = new File(fileName) val in = new FileInputStream(file) val bytes = new Array[Byte](file.length.toInt) in.read(bytes) in.close()
寫(xiě)入文本文件
Scala沒(méi)有內(nèi)建對(duì)寫(xiě)入文件的支持,可借助java進(jìn)行文件寫(xiě)入操作例如使用java.io.PrintWriter
/** * Scala寫(xiě)入文借助java的PrintWriter */ def write(): Unit = { val out = new PrintWriter("C:\\Users\\DonnieGao\\Desktop\\test.txt") for (i - 0 to 100) out.println(i) out.close() }
訪(fǎng)問(wèn)文件目錄
Scala中沒(méi)有直接訪(fǎng)問(wèn)某個(gè)目錄下的所有文件的方式或者遞歸遍歷有目錄的類(lèi)
/** * 使用java列舉下所有的文件夾 * @param dir 文件目錄路徑 */ def dir(dir:String) = { val dirFile = new File(dir) val arrayFile= dirFile.listFiles() for (i - arrayFile){println(arrayFile.toBuffer)} }
序列化
在java中聲明一個(gè)可以被序列號(hào)的類(lèi)通常是下面這種:
public class Person implements java.io.Serializable { private static final long serialVersionUID = 4436475322569107137L; }
Scala中聲明一個(gè)可以被序列化的類(lèi)通常是下面這種:
@SerialVersionUID(12356L) class ReadFile extends Serializable { }
正則表達(dá)式
Scala中提供了正則操作處理scala.util.matching.Regex讓這件事情可以變得簡(jiǎn)單。構(gòu)造一個(gè)Regex對(duì)象,用String類(lèi)的r方法即可
object RegexDemo { def main(args: Array[String]): Unit = { // 初始化正則對(duì)象 val numPattern = "[0-9]+".r val regex = "13 welcome to beijing" // findAllIn方法返回遍歷所有匹配的迭代器,可以在for循環(huán)中使用 for (matchString - numPattern.findAllIn(regex)) { println(matchString) } // 查詢(xún)字符串首個(gè)匹配項(xiàng) println(numPattern.findFirstIn(regex)) // 檢查某個(gè)字符串的開(kāi)始部分能匹配,可以使用findPrefixOf println(numPattern.findPrefixOf(regex)) }
總結(jié)
以上所述是小編給大家介紹的Scala的文件讀寫(xiě)操作與正則表達(dá)式 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
標(biāo)簽:涼山 哈密 常州 泰安 湖州 本溪 大興安嶺
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Scala的文件讀寫(xiě)操作與正則表達(dá)式》,本文關(guān)鍵詞 Scala,的,文件,讀寫(xiě),操作,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。