package com.xrx
import java.io.{File, FileReader, PrintWriter, Reader}
import scala.io.Source
object FileHelper{
def write(filePath: String, fileName: String, fileContext: String): String = {
val writer = new PrintWriter(new File(filePath + "/" + fileName))
writer.write(fileContext)
writer.close()
println("写入成功")
return "写入成功"
}
def read(filePath: String):String={
val source = Source.fromFile(filePath)
val content = source.mkString
source.close()
println("读取成功")
return content
}
}
object DemoWrite {
def main(args: Array[String]): Unit = {
val context = FileHelper.write("/Users/xrx/Documents", "text.txt", "你好")
println(context)
val content = FileHelper.read("text.txt")
println(content)
}
}
【scala】scala写入文件,读取文件内容
于 2024-02-17 02:32:15 首次发布