Spark基础-Scala文件操作

核心
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的内容
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值