1

I'm trying to create a CSV file with java contains some data in Arabic letters but when I open the file I found Arabic letters appears as symbols. here is my code :

String csvFilePath = "test.csv";
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(csvFilePath));
fileWriter.write("الاسم ، السن ، العنوان");

and data in CSV file appears like that

ط´ط±ظٹط·

so How can I solve this issue ?

  • If you rename the file from .csv to .txt does it then show correctly? Besides UTF-8 there is also the Right-To-Left control and CSV is also column based. – Joop Eggen Jul 14 '20 at 12:04

3 Answers3

3

As mentioned in Write a file in UTF-8 using FileWriter (Java)? you should use OutputStreamWriter and FileOutputStream instead of FileWriter and specify the encoding:

String csvFilePath = "test.csv";
FileOutputStream file = new FileOutputStream(csvFilePath);
OutputStreamWriter fileWriter = new OutputStreamWriter(file, StandardCharsets.UTF_8);
fileWriter.write("الاسم ، السن ، العنوان");
fileWriter.close();

Edit after comment:

To make sure the source code's encoding is not the problem, escape your unicode string like this:

fileWriter.write("\u0627\u0644\u0627\u0633\u0645\u0020\u060c\u0020\u0627\u0644\u0633\u0646\u0020\u060c\u0020\u0627\u0644\u0639\u0646\u0648\u0627\u0646");
fcdt
  • 2,371
  • 5
  • 14
  • 26
0

In my case, the file is defined as follows.

String file = fileName+"."+fileExtension;

The variable fileName is locale-dependent, i.e in Arabic it contains "تصدير" and in English "export". The variable fileExtension always contains "csv".

Now, the problem is that the variable file looks like "csv.تصدير" in Arabic and "export.csv" in English.

Is this the correct way for a file in Arabic or should it look like "تصدير.csv"?

Patrick
  • 136
  • 2
  • 9
0

As others have mentioned: use an OutputStreamWriter. Additionally you can write “\uFEFF” as a first character. This will act as a byte order mark and it helps applications like excel to know which encoding you used.

Kristof Neirynck
  • 3,934
  • 1
  • 33
  • 47