class HDFSWordCount { def main (args: Array[String]) { if (args.length > 0){ for (line <- Source.fromFile(args(0)).getLines().toList) //求最大长度 println(line.length +" " +line) } else Console.err.print("please enter fileName") } //对文件的每行记录打印格式的字符量化 def widthOfLength(s: String) = s.length.toString.length if (args.length > 0){ val lines = Source.fromFile(args(0)).getLines.toList val longetLine = lines.reduceLeft( (a, b) => if (a.length > b.length) a else b ) val maxWidth = widthOfLength(longetLine) for (line <- lines){ val numSpace = maxWidth widthOfLength(line) val padding = " " * numSpace println(padding +line.length + "|" +line) } } }
时间: 2024-10-05 04:45:04