Save CSV file with encoding UTF-8 with Java

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 ?

Add Comment
1 Answer(s)

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"); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.