核心
scala文件操作
文件操作的大概流程
package com.xlucas
import java.io._
import scala.io.Source
/**
* Created by xlucas on 2017/9/4 0004.
*/
@SerialVersionUID(99L) class PaicMis(val name:String) extends Serializable //定义序列化类
object FileOop extends App {
val paic = new PaicMis("xlucas")
def serializeMen[T](o: T): Array[Byte] = {
val bos = new ByteArrayOutputStream() //内存输出流,和磁盘输出流从操作层面上讲是一样的
val bos1 = new FileOutputStream("E:\\spark\\spark.txt")
val oos = new ObjectOutputStream(bos)
oos.writeObject(o)
oos.close()
bos.toByteArray
}
def serializeFile[T](o: T) = {
val bos = new FileOutputStream("E:\\spark\\spark.txt") //输入文件序列化
val oos = new ObjectOutputStream(bos)
oos.writeObject(o)
oos.close()
}
println(serializeMen(paic))
def deserializeMen[T](bytes: Array[Byte]): T = {
val bis = new ByteArrayInputStream(bytes)//内存反序列化
val ios = new ObjectInputStream(bis)
ios.readObject().asInstanceOf[T]
}
println(serializeMen(paic))
def deserializeFile[T](bytes: Array[Byte]): T = {
val bis = new FileInputStream("E:\\spark\\spark.txt")//文件反序列化
val ios = new ObjectInputStream(bis)
ios.readObject().asInstanceOf[T]
}
println(deserializeMen[PaicMis](serializeMen[PaicMis](paic)).name)
serializeFile(paic)
println(deserializeFile[PaicMis](null).name)
for (line <- Source.fromFile("E:\\spark\\file.txt","UTF-8").getLines()) println(line) //scala读取文件的内容
println(Source.fromFile("E:\\spark\\file.txt","UTF-8").mkString)//scala读取文件的内容
println(Source.fromURL("http://blog.csdn.net/paicMis/article/month/2017/09","utf-8").mkString)//scala获取url的内容
}