用Scala文件内容的生成本地的一个文件

格式: url time traffic 例如: http://www.baidu.com [2018-12-08 22:00:00] 30
生成一些错乱数据比如,时间格式不对,流量不为数字
package github.fenian7788.io

import java.io.{File, PrintWriter}
object GenerateFileApp {
val years = Array(“2010”, “2011”, “2012”, “2013”, “2014”, “2015”, “2016”, “2017”, “2018”, “2019”, “2009”, “2008”, “2010”, “2018”, “2016”, “2004”, “2001”, “2006”, “2013”, “2003”, “2008”, “2014”, “2006”, “2007”, “2008”, “2000”, “2005”, “5002”, “2001”, “2010”, “2016”,“2007”, “2008”, “2000”, “2005”, “5002”, “2001”, “2018”, “2016”, “2004”, “2001”, “2006”, “2013”)
val urls = Array(“https://blog.csdn.net/weixin_39702831/article/details/82722475”, “https://blog.csdn.net/qq_43688472/article/details/84193735”, “https://blog.csdn.net/qq_43688472/article/details/84193735”, “https://blog.csdn.net/qq_43688472/article/details/84193735”, “https://blog.csdn.net/qq_43688472/article/details/84102325”, “https://mp.csdn.net/mdeditor/84203650#”, “https://blog.csdn.net/qq_43688472/article/details/84203650”, “https://blog.csdn.net/qq_43688472/article/details/84193735”, “https://blog.csdn.net/qq_43688472/article/details/84238903”, “https://mp.csdn.net/mdeditor/84307884#”, “https://blog.csdn.net/qq_43688472/article/details/84307884”, “https://blog.csdn.net/qq_43688472/article/details/84451902”, “https://blog.csdn.net/qq_43688472/article/details/84472954”, "http://blog.itpub.net/30089851/ ", “https://my.oschina.net/u/3962854/blog/2961493”, “https://blog.csdn.net/qq_43688472/article/details/84575518”, “https://blog.csdn.net/qq_43688472/article/details/84472954”, “https://blog.csdn.net/qq_43688472/article/details/84945334”, “https://blog.csdn.net/qq_43688472/article/details/84940873”, “https://blog.csdn.net/qq_43688472/article/details/85012965”, “https://fenian7788.github.io/categories/作业/”, “https://blog.csdn.net/qq_43688472/article/details/85047190”, “https://blog.csdn.net/qq_43688472/article/details/85012965”, “https://blog.csdn.net/qq_24073707/article/details/80988329”, “https://blog.csdn.net/qq_24073707/article/details/80800158”, “https://blog.csdn.net/qq_24073707/article/details/80716279”, “https://blog.csdn.net/qq_24073707/article/details/80665991”, “https://blog.csdn.net/qq_24073707/article/details/80665991”, “https://blog.csdn.net/qq_24073707/article/details/80665991”, “https://blog.csdn.net/qq_24073707/article/details/80659742”, “https://blog.csdn.net/qq_24073707/article/details/80658301”, “https://item.jd.com/4477667.html”, “https://item.jd.com/25619900612.html”, “https://item.jd.com/10803164977.html”, “https://item.jd.com/11026973034.html”, “https://item.jd.com/34091847083.html”, “https://item.jd.com/34091847083.html”, “https://item.jd.com/29280987593.html”, “https://item.jd.com/4803334.html”, “https://item.jd.com/4706366.html”, “https://item.jd.com/26838388932.html”, “https://item.jd.com/34308836529.html”, “https://item.jd.com/4315566.html”, “https://item.jd.com/19997245287.html”, “https://item.jd.com/1226464483.html”, “https://item.jd.com/100001625726.html”, “https://takefired.taobao.com/?spm=a217m.8316598.711275.5.426d33d5KRgTIh”, “https://ccaik.taobao.com/?spm=a217m.8316598.711275.6.426d33d5KRgTIh”, “https://tandf.taobao.com/?spm=a217m.8316598.711275.8.426d33d5KRgTIh”, “https://rock-honey.taobao.com/?spm=a217m.8316598.711275.9.426d33d5KRgTIh”, “https://shop159942490.taobao.com/?spm=a217m.8316598.711712.8.426d33d5KRgTIh”, “https://unawares.taobao.com/?spm=a217m.8316598.682348.7.426d33d5KRgTIh”, “https://wengna.taobao.com/?spm=a217m.8316598.682375.7.426d33d5KRgTIh”, “https://12cmlook.taobao.com/?spm=a217m.8316598.682375.9.426d33d5KRgTIh”, “https://hizero.taobao.com/?spm=a217m.8316598.682372.3.426d33d5KRgTIh”, “https://item.taobao.com/item.htm?spm=a230r.1.14.37.15da4674Xnq4Zx&id=576801888968&ns=1&abbucket=11#detail”, “https://item.taobao.com/item.htm?spm=a230r.1.14.194.15da4674Xnq4Zx&id=562541312363&ns=1&abbucket=11#detail”, “https://item.taobao.com/item.htm?spm=a230r.1.14.236.15da4674Xnq4Zx&id=562311126790&ns=1&abbucket=11#detail”, “https://item.taobao.com/item.htm?spm=a230r.1.14.272.15da4674Xnq4Zx&id=575296595111&ns=1&abbucket=11#detail”)

val filePath = “E:\data.txt”
注意:要在本地文件创建一个data.txt文件

def main(args: Array[String]): Unit = {

val writer = new PrintWriter(new File(filePath))
for (i <- 1 to 100) {
  writer.println(generateLines)
}
writer.flush()
writer.close()

}
def generateLines(): String = {
var sb: StringBuilder = new StringBuilder()
sb.append(generateRandomUrl())
.append("\t")
.append(generateRandomDate())
.append("\t")
.append(generateRandomNum2(1000))
sb.toString()
}

//生成随机数
def generateRandomNum(range: Int) = {
(new util.Random).nextInt(range) + 1
}

def generateRandomNum2(range: Int) = {
val num = (new util.Random).nextInt(range)
if (num % 23 == 0) generateRandomNum(9).toString + “-” else generateRandomNum(100).toString
}

//会缺大括号的生成年份
def generateRandomDate() = {
var sb: StringBuilder = new StringBuilder()
val randomNum = generateRandomNum(1000)
if (randomNum % 97 != 0) sb.append("[") else sb.append(" “)
//年份
sb.append(years((new util.Random).nextInt(years.length))).append(”-")
//月份
sb.append("%02d".format(generateRandomNum(12))).append("-")
//日期
sb.append("%02d".format(generateRandomNum(30))).append(" “)
//小时
sb.append(”%02d".format(generateRandomNum(23))).append("?
//分钟
sb.append("%02d".format(generateRandomNum(59))).append("?
//秒
sb.append("%02d".format(generateRandomNum(59)))
if (randomNum % 89 != 0) sb.append("]") else sb.append(" ")
sb.toString()
}

//从urls随机获取一个地址
def generateRandomUrl(): String = {
urls((new util.Random).nextInt(urls.length))
}
}

结果:在这里插入图片描述

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐