1. 文件读取的多种方式
1.1 使用Scala原生Source类
import scala.io.Source
import java.nio.charset.{Charset, StandardCharsets}
object FileReadingExamples {
def readEntireFile(filePath: String): String = {
val source = Source.fromFile(filePath)
try {
source.mkString
} finally {
source.close()
}
}
def readLinesLazily(filePath: String): Iterator[String] = {
Source.fromFile(filePath).getLines()
}
def processLines(filePath: String): List[String] = {
val source = Source.fromFile(filePath)
try {
source.getLines()
.map(_.trim)
.filter(_.nonEmpty)
.toList
} finally {
source.close()
}
}
def readWithEncoding(filePath: String, encoding: String = "UTF-8"): String = {
implicit val codec = scala.io.Codec(encoding)
val source = Source.fromFile(filePath)
try {
source.mkString
} finally {
source.close()
}
}
def readInBatches(filePath: String, batchSize: Int = 1000): Iterator[List[String]] = {
val lines = Source.fromFile(filePath).getLines()
new Iterator[List[String]] {
def hasNext: Boolean = lines.hasNext
def next(): List[String] = {
var batch = List.empty[String]
var count = 0
while (lines.hasNext && count < batchSize) {
batch = lines.next() :: batch
count += 1
}
batch.reverse
}
}
}
}
1.2 使用Java NIO(更高效的方式)
import java.nio.file.{Files, Paths, StandardOpenOption}
import java.nio.charset.StandardCharsets
import scala.collection.JavaConverters._
object JavaNIOExamples {
def readAllLines(filePath: String): List[String] = {
Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8)
.asScala
.toList
}
def readLinesStream(filePath: String): Iterator[String] = {
Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)
.iterator()
.asScala
}
def readBytes(filePath: String): Array[Byte] = {
Files.readAllBytes(Paths.get(filePath))
}
}
1.3 使用scala.util.Using(资源管理)
import scala.util.{Try, Using}
import scala.io.Source
object UsingResourceExamples {
def readFileSafely(filePath: String): Try[String] = {
Using(Source.fromFile(filePath)) { source =>
source.mkString
}
}
def mergeFiles(file1: String, file2: String): Try[String] = {
Using.Manager { use =>
val source1 = use(Source.fromFile(file1))
val source2 = use(Source.fromFile(file2))
source1.mkString + "\n" + source2.mkString
}
}
case class DatabaseConnection(url: String) extends AutoCloseable {
def query(sql: String): String = s"Result of: $sql"
override def close(): Unit = println("Closing database connection")
}
def withDatabase[A](url: String)(f: DatabaseConnection => A): Try[A] = {
Using(new DatabaseConnection(url))(f)
}
}
2. 文件写入的多种方式
2.1 基本写入操作
import java.io.{File, PrintWriter, FileWriter, BufferedWriter}
import java.nio.file.{Files, Paths, StandardOpenOption}
object FileWritingExamples {
def writeWithPrintWriter(filePath: String, content: String): Unit = {
val writer = new PrintWriter(new File(filePath))
try {
writer.print(content)
writer.flush()
} finally {
writer.close()
}
}
def appendToFile(filePath: String, content: String): Unit = {
val writer = new FileWriter(filePath, true)
try {
writer.write(content + System.lineSeparator())
writer.flush()
} finally {
writer.close()
}
}
def writeWithBuffering(filePath: String, lines: List[String]): Unit = {
val writer = new BufferedWriter(new FileWriter(filePath))
try {
lines.foreach { line =>
writer.write(line)
writer.newLine()
}
writer.flush()
} finally {
writer.close()
}
}
def writeWithNIO(filePath: String, content: String): Unit = {
Files.write(
Paths.get(filePath),
content.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE
)
}
def appendWithNIO(filePath: String, content: String): Unit = {
Files.write(
Paths.get(filePath),
content.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND
)
}
def writeLines(filePath: String, lines: List[String]): Unit = {
import java.nio.charset.StandardCharsets
import scala.collection.JavaConverters._
Files.write(
Paths.get(filePath),
lines.asJava,
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING
)
}
}