'How to write a txt file from spark scala Dataframe

I want to write a Dataframe as a .txt file along with headers and delimited with | and text separator as ".I know we can create it as .CSV file but I do not want the extension to be .CSV

sample txt file below

"RollNum"|"Name"|"subject"|"Marks"
"12345"|"aaa"|"science"|"50"
"12345"|"aaa"|"social"|"45"
"12345"|"aaa"|"maths"|"89"


Solution 1:[1]

You can use text, but text can accept only one column ( so basically you need to convert all the columns as a single column before you write)

df.write.text("testpath")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shankar