scala IO operations

Java 1.scala mainly used in the I / O class (java.io.File)

public static final String pathSeparator constants representing the path delimiter (windows: ';')

public static final String separator represents a constant path delimiter (windows: '\')

public File (String pathname) Creates a construction File class object, passing the full path

public boolean createNewFile () throws IOException ordinary create a new file

Whether public boolean exists () Ordinary file exists is determined

public boolean delete () Ordinary delete files

Whether public boolean isDirectory () Normal determine whether a given path is a directory

public long length () Returns the file size of the ordinary

public String [] list () List All general contents of the specified directory, but name

public File [] listFiles () List All general contents of the specified directory will be listed path.

public boolean mkdir () to create a common directory

public boolean renameTo (File dest) common to rename the existing file

 

2. Small example:

import java.io.File
import scala.io.Source
import java.io.PrintWriter


val filePath = new File("C:\\Users\\peanut\\Desktop\\apple.txt")

val source=Source.fromFile(filePath)
val source2=Source.fromFile(filePath,"UTF-8")
val source3 = Source.fromURL("www.baidu.com","UTF-8")
val source4 = Source.fromString("i like strawberry")


//读取文件中所有行,返回一个迭代器
val lineIterator = source.getLines()
lineIterator.foreach(println)

//读取文件中所有行,toArray返回一个数组
val lineArray = source.getLines().toArray
for(ele <- lineArray){
println("line: "+ele)
}

//读取文件中所有行,mkstring返回一个字符串
val lineString = source.getLines().mkString //文件不能过大


//将文件内容以空格分割,返回一个数组
val arrayTokens = source.mkString.split("\\s+")

source.close() //关闭流


//写文件

val out =new PrintWriter("C:\\Users\\peanut\\Desktop\\appleWrite.txt")
for (i <- 1 to 10){
//out.println("hello")
out.write("Apple")
}
out.close()//关闭打印流

 

3. Small Example 2:

val path = new File("C:\\Users\\peanut\\Desktop\\myFile")

for(file <- getFile(path)){
val filePath=file.getAbsolutePath //获取绝对路径

System.out.println("getAbsolutePath: " + filePath)
val fileName=file.getName //获取文件名
System.out.println("fileName: " + fileName)
}


val path2 = new File("C:\\Users\\peanut\\Desktop\\apple.txt")

try {
path2.createNewFile()
println("create file success ")
} catch {
case e:Exception => println("can't create "+path2)
}

if(path2.exists()){
path2.delete()
println("delete file success!")
}



/**
* 遍历出指定目录下的全部文件
* @param file 指定目录
* @return Array[File]
*/
def getFile(file: File): Array[File] = {

    val files = file.listFiles().filter(!_.isDirectory) //过滤掉目录
    //.filter(t => t.toString.endsWith(".txt") || t.toString.endsWith(".json") ) //过滤保留特定后缀名的文件

    //递归遍历子目录
    val files2=file.listFiles().filter(_.isDirectory).flatMap(getFile)
    files ++ files2
}


/**
* 遍历出指定目录下的所有目录
* @param file 指定目录
* @return Array[File]
*/
def getDir(file: File): Array[File] = {

    val dirs = file.listFiles().filter(_.isDirectory) //只保留目录

    //递归遍历子目录
    val dirs2=files.flatMap(getDir)
    dirs++ dirs2
}

 

Published 53 original articles · won praise 40 · views 40000 +

Guess you like

Origin blog.csdn.net/u012761191/article/details/105310765