0

I want to save values into txtfile instead of println.

val movieMatrix = new RowMatrix(movieVectors) 
 val movieMatrixSummary =movieMatrix.computeColumnSummaryStatistics() 

val userMatrix = new RowMatrix(userVectors) 
val userMatrixSummary = userMatrix.computeColumnSummaryStatistics()
println("Movie factors mean: " + movieMatrixSummary.mean)
println("Movie factors variance: " + movieMatrixSummary.variance) 
println("User factors mean: " + userMatrixSummary.mean) 
println("User factors variance: " + userMatrixSummary.variance)

how to save values?

1 Answers1

1

You can use the Java PrintWriter or FileWriter to write values to a text file.

// PrintWriter
import java.io._
val pw = new PrintWriter(new File("summary.txt" ))
pw.write("Movie factors mean: " + movieMatrixSummary.mean + "\n")
pw.close

// FileWriter
val file = new File(filepath)
val bw = new BufferedWriter(new FileWriter(file))
bw.write("Movie factors mean: " + movieMatrixSummary.mean + "\n")
bw.close()

It is generally better to use FileWriter as it throws IOExceptions, whereas PrintWriter doesn't throw exceptions, and instead sets Boolean flags that can be checked.