如何找到文件中的行数和不同元素并将它们写入header,Scala
How to find the number of lines and different elements in a file and writing them on header, Scala
我有类似的东西:
object Example_01_IO {
val s = Source.fromFile("example_01.txt")
val source = s.getLines()
val destination = new PrintWriter(new File("des_example_01.txt"))
var nrVariables: Int = 0
var nrLines: Int = 0
// here are the extracted lines from example_01 that fulfills some conditions.
val linesToWrite: Iterator[String] = ...
def main(args: Array[String]): Unit = {
//Here is the header that I want to write in a destination file
destination.write("des_example_01.txt \n")
destination.write("Nr. of Variables and Lines: " + nrVariables + " " + nrLines + "\n")
for(line <- linesToWrite) {
println(line)
destination.write(line)
destination.write("\n")
nrLines += 1
}
s.close()
destination.close()
}
我需要将 nrVariables
和 nrLines
的值写入目标文件的 header(例如,在第二行)。有没有可能在开始写其他行之前计算这两个值?
非常欢迎任何帮助或参考。谢谢。
好吧,Source.fromFile 不能重复使用,下面那个可以用:
package example
import java.io.PrintWriter
import scala.io.Source
import java.io.File
object Example_01_IO {
def s = Source.fromFile("/tmp/example_01.txt") // notice def everywhere, looks like Source.fromFile could not be reused :(
def source = s.getLines()
val destination = new PrintWriter(new File("/tmp/des_example_01.txt"))
var nrVariables: Int = 0
var nrLines: Int = 0
// here are the extracted lines from example_01 that fulfills some conditions.
def linesToWrite: Iterator[String] = source.filter { s => s.contains("a") }
def main(args: Array[String]): Unit = {
linesToWrite.foreach { s =>
nrLines += 1
if (s contains "variable") {
nrVariables += 1
}
}
//Here is the header that I want to write in a destination file
destination.write("des_example_01.txt \n")
destination.write("Nr. of Variables and Lines: " + nrVariables + " " + nrLines + "\n")
for(line <- linesToWrite) {
println(line)
destination.write(line)
destination.write("\n")
/*nrLines += 1*/
}
s.close()
destination.close()
}
}
我有类似的东西:
object Example_01_IO {
val s = Source.fromFile("example_01.txt")
val source = s.getLines()
val destination = new PrintWriter(new File("des_example_01.txt"))
var nrVariables: Int = 0
var nrLines: Int = 0
// here are the extracted lines from example_01 that fulfills some conditions.
val linesToWrite: Iterator[String] = ...
def main(args: Array[String]): Unit = {
//Here is the header that I want to write in a destination file
destination.write("des_example_01.txt \n")
destination.write("Nr. of Variables and Lines: " + nrVariables + " " + nrLines + "\n")
for(line <- linesToWrite) {
println(line)
destination.write(line)
destination.write("\n")
nrLines += 1
}
s.close()
destination.close()
}
我需要将 nrVariables
和 nrLines
的值写入目标文件的 header(例如,在第二行)。有没有可能在开始写其他行之前计算这两个值?
非常欢迎任何帮助或参考。谢谢。
好吧,Source.fromFile 不能重复使用,下面那个可以用:
package example
import java.io.PrintWriter
import scala.io.Source
import java.io.File
object Example_01_IO {
def s = Source.fromFile("/tmp/example_01.txt") // notice def everywhere, looks like Source.fromFile could not be reused :(
def source = s.getLines()
val destination = new PrintWriter(new File("/tmp/des_example_01.txt"))
var nrVariables: Int = 0
var nrLines: Int = 0
// here are the extracted lines from example_01 that fulfills some conditions.
def linesToWrite: Iterator[String] = source.filter { s => s.contains("a") }
def main(args: Array[String]): Unit = {
linesToWrite.foreach { s =>
nrLines += 1
if (s contains "variable") {
nrVariables += 1
}
}
//Here is the header that I want to write in a destination file
destination.write("des_example_01.txt \n")
destination.write("Nr. of Variables and Lines: " + nrVariables + " " + nrLines + "\n")
for(line <- linesToWrite) {
println(line)
destination.write(line)
destination.write("\n")
/*nrLines += 1*/
}
s.close()
destination.close()
}
}