【scala】scala写入文件,读取文件内容

╰+攻爆jí腚メ 2024-04-17 11:27 138阅读 0赞
  1. package com.xrx
  2. import java.io.{
  3. File, FileReader, PrintWriter, Reader}
  4. import scala.io.Source
  5. object FileHelper{
  6. def write(filePath: String, fileName: String, fileContext: String): String = {
  7. val writer = new PrintWriter(new File(filePath + "/" + fileName))
  8. writer.write(fileContext)
  9. writer.close()
  10. println("写入成功")
  11. return "写入成功"
  12. }
  13. def read(filePath: String):String={
  14. val source = Source.fromFile(filePath)
  15. val content = source.mkString
  16. source.close()
  17. println("读取成功")
  18. return content
  19. }
  20. }
  21. object DemoWrite {
  22. def main(args: Array[String]): Unit = {
  23. val context = FileHelper.write("/Users/xrx/Documents", "text.txt", "你好")
  24. println(context)
  25. val content = FileHelper.read("text.txt")
  26. println(content)
  27. }
  28. }

发表评论

表情:
评论列表 (有 0 条评论,138人围观)

还没有评论,来说两句吧...

相关阅读