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 /** * 一行行读取文件的内容 * * @param resource 文件路径 * @param code 文件编...
import scala.io.Source def readFile(filePath: String): String = { val source = Source.fromFile(filePath) val content = source.mkString source.close() content } val filePath = "path/to/file.txt" val fileContent = readFile(filePath) println(fileContent) 上述代码中,readFile函数接收一个文...
也可以使用迭代器应用toArray或toBuffer方法,将这些行放到数组力或者数组缓冲行中,若想将读取的的文件作为一个字符串,只需valconents = source.mkString 下面是简单的代码实例:读取桌面上的a.txtobjectReadFile{defmain(args:Array[String]):Unit= {valread =newReadFile()valresource:String="C:\\Users\\DonnieG...
write:直接写入,不会换行 importjava.io.{File,PrintWriter} 案例——println方法 objectreadDemo{defmain(args:Array[String]):Unit={importjava.io.{File,PrintWriter}valwrite=newPrintWriter(newFile("d://Hello.txt"))//输入文件名称for(i<-1to100){write.println(i)}write.close()}} 案例——write方法...
*/classFileUtil{defreadFromFile(path:String)={valsource=Source.fromFile(path);valfile=source.getLines();// 返回值为Iterator[String]file.foreach{line=>println(line);}source.close();}defreadFromUrl(url:String)={try{valsource=Source.fromURL(url);valfile=source.getLines();// 返回值为Iterator...
def main(args: Array[String]): Unit ={ //1 readfor( i<- Source.fromFile("test.dat").getLines().toArray){ println(i) } // 2 read println(Source.fromFile("test.dat").mkString) //1write val out=newPrintWriter("test.dat")for(i<-1 to 100) out.println(i) ...
conf object bvs { def main(args: Array[String]): Unit = { // 读取配置文件 val content = Source.fromFile("/bvs/bvs.conf").getLines val conf = new conf() val confmap = conf.parser(content.mkString("\n")) // Set(rulesfile, device2, writelocal, logfile, device1, interval) // ...
to是指数值串的终点。 def readTextFile(filePath: String): DataStream[String] 获取用户定义的某路径下的文本文件数据,作为输入流数据。 filePath是指文本文件的路径。 charsetName指的是编码格式的名字。 def readTextFile(filePath: String, charsetName: String): DataStream[String] ...
首先在MyPredef写一个String的隐式转换函数; packageday04importscala.io.SourceclassReader(valpath:String){// 读取文件得到文件内容返回defread=Source.fromFile(path).mkString}objectReader{defmain(args:Array[String]):Unit={valpath:String="/tmp/scala/ip.txt"// val reader = new Reader(path)// 通过...
val source = Source.fromFile("C://Users//Administrator//Desktop//test.txt", "UTF-8")for(c <- source) print(c) 1. 三、从URL以及字符串中读取字符 val source = Source.fromURL("http://www.baidu.com", "UTF-8")val source = Source.fromString("Hello World") ...