作业2:通过Scala以固定格式写数据到文件

题目要求:


写数据到文件的操作:

    val randomFile = new RandomAccessFile("Path\\outputFile.txt","rw")
    val fileLength = randomFile.length; //得到文件长度
    randomFile.seek(fileLength);//指针指向文件末尾
    randomFile.writeBytes(getlinecont().toString()); //写入数据

源码:

package com.ruozedata
import java.io.RandomAccessFile
import scala.collection.mutable.ArrayBuffer
object fileIO {
  var randomnum=ArrayBuffer("00","01","02","03","04","05","06","07","08","09")
  var traffic=ArrayBuffer("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
  def main(args: Array[String]): Unit = {
    val randomFile = new RandomAccessFile("G:\\bigdata\\[201805发布版]若泽数据-零基础大数据第4期\\软件\\IDEA_workspace\\g-719\\src\\main\\scala\\com\\ruozedata\\outputFile.txt","rw")
    val fileLength = randomFile.length; //得到文件长度
    randomFile.seek(fileLength);//指针指向文件末尾
    for(a<- 10 until 60)
      randomnum+=""+a
    for(b<- -10000 to 20000)
      traffic+=""+b
    for(i<- 0 to 75000) {
      randomFile.writeBytes(getlinecont().toString()); //写入数据
    }
    randomFile.close();
  }
  def getlinecont():StringBuilder={
    var linecontent=new StringBuilder()
    val doman = Array("www.ruozedata.com","www.zhibo8.com","www.dongqiudi.com")
    linecontent.append(doman((new util.Random).nextInt(3))+"\t")
    linecontent.append(traffic((new util.Random).nextInt(traffic.size))+"\t")
    var day=(new util.Random).nextInt(30)
    var hour=(new util.Random).nextInt(24)
    var min=(new util.Random).nextInt(60)
    var sec=(new util.Random).nextInt(60)
    var time="[2018-07-"+randomnum(day+1)+" "+randomnum(hour)+":"+randomnum(min)+":"+randomnum(sec)+"]"
    linecontent.append(time+"\n")
    linecontent
  }
}

操作结果:形成3.15m的txt文本,以下是其中的部分数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值