# Scala简单计算实例，其在数据分析方面的优势体会

`package com.bill.www/*** Created by Bill on 2016/2/3.* 目的：用scala实现简单的数据计算* 源文件：接口记录条数20条，包括时间戳和浮点型数据* 执行方式：scala ReadFile.scala "E:\\spark\\data\\i_22_221000000073_L_20151016\\i_22_221000000073_L_20151016_223458.dat"* 开发环境：win10 + IJ IDEA15.0.2 + scala2.11.7 + java1.7*/import scala.collection.mutableimport scala.io.Sourceobject ReadFile {def main(args: Array[String]) {println("hello-------------------------------------------------------")// 读取文件并逐行打印其内容和长度if(args.length > 0) {for( line <- Source.fromFile(args(0)).getLines)print(line.length + "\t-- " +line + "\n")}elseConsole.err.println("Please enter a file or file with directory")println("--------------------------------------------------------1st.")// 用ArrayBuffer来存一列数据，通过对对象的一些操作来实现各种计算val nums = mutable.ArrayBuffer[Float]()if(args.length > 0) {for( line <- Source.fromFile(args(0)).getLines) {// 逐行处理// 拆分字段，类型转换，追加到可变数组nums += line.split(" ")(1).toFloat}}elseConsole.err.println("Please enter a file or file with directory")// 做一些简单的计算，并输出计算结果// common calculateprintln(nums.sum)println(nums.length)println(nums.max)println(nums.min)// averageprintln(nums.sum/nums.length)println("--------------------------------------------------------2nd.")// sort descnums.sorted.reverse foreach(println)println("--------------------------------------------------------end.")}}`

`hello-------------------------------------------------------21-- 22:34:58.0000 23.929920-- 22:34:58.0005 23.92721-- 22:34:58.0010 23.927721-- 22:34:58.0015 23.928420-- 22:34:58.0020 23.92721-- 22:34:58.0025 23.925620-- 22:34:58.0030 23.92720-- 22:34:58.0035 23.92721-- 22:34:58.0040 23.926321-- 22:34:58.0045 23.928421-- 22:34:58.0050 23.927721-- 22:34:58.0055 23.926321-- 22:34:58.0060 23.929121-- 22:34:58.0065 23.925621-- 22:34:58.0070 23.927721-- 22:34:58.0075 23.925621-- 22:34:58.0080 23.924121-- 22:34:58.0085 23.926320-- 22:34:58.0090 23.92721-- 22:34:58.0095 23.9263--------------------------------------------------------1st.478.542023.929923.924123.927--------------------------------------------------------2nd.23.929923.929123.928423.928423.927723.927723.927723.92723.92723.92723.92723.92723.926323.926323.926323.926323.925623.925623.925623.9241--------------------------------------------------------end.`

初学阶段的学习交流。