0

I have 1 input file with n lines. How can I create n output files from n lines?

I just know

for (line <- Source.fromFile(filePath).getLines) {
  println(line)
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
BlackZero
  • 23
  • 3
  • 2
    Possible duplicate of [How to write to a file in Scala?](http://stackoverflow.com/questions/4604237/how-to-write-to-a-file-in-scala) – soote Nov 11 '16 at 03:58
  • Did you try the search function on this site? – maasg Nov 11 '16 at 13:39

1 Answers1

0

If the question is to know how to write the file, this can be done in two approaches.

1) By using PrintWriter

val writersample = new PrintWriter(new File("sample.txt" ))
writersample.write("put the content you want write")
//you can write as many lines as you want
writersample.close

2) By using FileWriter

val file = new File("sample.txt")
val bufferw = new BufferedWriter(new FileWriter(file))
bufferw.write("whatever you want to write here")
bufferw.close()

If you are looking to write n different files, probably you need to repeat the code by overriding the filename eachtime in a loop.

differences between both approaches can be read @ https://coderanch.com/t/418148/certification/Information-PrintWriter-FileWriter

Please let me know if you are looking for different answer than this.