Search code examples
javaswingcsvjtextarea

How to save complete data of JTextArea in to single cell of csv file


Iam saving JTextArea data in to csv file, but application is not saving the entire data in single cell.

for example: In JTextArea i added data as below

apple
mango
banana
guava

In .csv file it is saving in four rows instead of single row.

below is my code how iam saving the data.

String address ;
address = textArea.getText();
writer.append(address);
writer.append(',');

Can anyone suggest how to fix the issue?

Thanks


Solution

  • Why not replace the newline characters of the JTextArea with the commas to give you a single CSV row:

    writer.append(address.replace("\n", ","));
    

    Output:

    apple,mango,banana,guava