Examples of scala’s method of reading txt files

Time:2019-9-8

The file formats are as follows

   

package txt
 
object ReadTxt {
 
 def readFromTxtByLine(filePath:String) = {
 // Importing Scala's IO package
 import scala.io.Source
 // Read the file with the specified UTF-8 character set. The first parameter can be a string or java.io.File
 val source = Source.fromFile(filePath, "UTF-8")
 // Or take all the lines in the file
 //val lineIterator = source.getLines()
 // Iterative printing of all rows
 //lineIterator.foreach()
 // Put all rows in an array
 val lines = source.getLines().toArray
 source.close()
 //println(lines.size)
 lines
 }
 
 def LackingFileUtil(array:Array[String],keyMap:Map[String,String]) = {
 // Traversing Array/Array Buffer with for loop and until
 // Make until a function provided by RichInt
 var lockMapList = List[Map[String,String]]()
 for (i <- 0 until array.length){
  if(i==0){
  val lineArray = array(i).trim.split("\|")
  lockMapList = lockMapList :+ Map("id"->lineArray(0).trim,"name"->lineArray(1).trim,"pass"->lineArray(2).trim)
  }else{
  val lineArray = array(i).split(",")
  if(lineArray.length==3){
   lockMapList = lockMapList :+ Map("id"->lineArray(0).trim,"name"->lineArray(1).trim,"pass"->lineArray(2).trim)
  }else if(lineArray.length==2){
   val pass = keyMap.getOrElse(lineArray(0).trim,lineArray(1).trim)
   lockMapList = lockMapList :+ Map("id"->lineArray(0).trim,"name"->lineArray(1).trim,"pass"->pass.trim)
  }else if(lineArray.length==1){
   lockMapList = lockMapList :+ Map("id"->lineArray(0).trim,"name"->"","pass"->"")
  }else{
   lockMapList = lockMapList :+ Map("id"->"-","name"->"","pass"->"")
  }
  }
  //println(array(i))
 }
 lockMapList
 
 // Jump through Array/Array Buffer
 /*for(i <- 0 until (array.length, 2))
  println(array(i))*/
 
 // Traversing Array/Array Buffer from the tail
 /*for(i <- (0 until array.length).reverse)
  println(array(i))*/
 
 // Traversing Array / Array Buffer with Enhanced for Loop
 /*for (e <- array){
  println(e)
  e.split(",").mkString
 }*/
 
 }
 
 def KeyFileUtil(array:Array[String]) = {
 var keyMapList = Map[String, String]()
 for (i <- 0 until array.length) {
  //println(array(i))
  val lineArray = array(i).trim.split(",")
  //println(lineArray.size)
  if(lineArray.size==2){
  keyMapList = keyMapList ++ Map(lineArray(0).trim -> lineArray(1).trim)
  }else if(lineArray.size==1){
  keyMapList = keyMapList ++ Map(lineArray(0).trim -> "")
  }else{
  keyMapList = keyMapList ++ Map("-" -> "")
  }
 }
 keyMapList
 }
 
 def isEmpty(s: String): Boolean = (s == null) || (s.size==0)
}

package txt
 
import scala.reflect.io.File
 
 
object App {
 
 def main(args: Array[String]): Unit = {
 
 val key = ReadTxt.readFromTxtByLine("D:"+File.separator+"workspace"+File.separator+"source.txt")
 //println(key.mkString)
 val keyMap = ReadTxt.KeyFileUtil(key)
 
 //println(keyMap)
 
 
 val lack = ReadTxt.readFromTxtByLine("D:"+File.separator+"workspace"+File.separator+"source1.txt")
 
 val lockMapList = ReadTxt.LackingFileUtil(lack,keyMap)
 
 println(lockMapList)
 
 
 }
}

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Server Core Knowledge

Catalog 1. Computer: a tool to assist human brain 2. Composition of computer hardware 3. power supply 4.CPU 5. memory 6. Classification of computer use 7. Introduction to common Internet servers 8. Server classification 1. Computer: a tool to assist human brain Nowadays people touch computers almost all the time! Whether it’s a desktop computer, […]