How to Write String to File in Scala

Write String to File in Scala

In this example, we will show simple example about, How to write string to file in scala. The program has been tested and shared in the post.

Example Code

import java.util.Scanner;

object WriteFileExample {
	def writeFile(fileName: String, content: String) {
		if(fileName != null && !fileName.isEmpty()) {
			var file = new File(fileName);
			var fileWriter = new FileWriter(file);
	def main(args: Array[String]) {
		var scanner = new Scanner(;
		var fileName = "abc.txt";
		println("Enter the Content");
		var content = scanner.nextLine();
		writeFile(fileName, content);
		println("The content has been written to file successfully...");


Enter the Content
This is Dinesh Krishnan
The content has been written to file successfully…

Hello, folks, I am a founder of I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.