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 ?

1
  • 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. Commented Jul 14, 2020 at 12:04

4 Answers 4

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");
Sign up to request clarification or add additional context in comments.

8 Comments

Even better: use try-with-resources. This makes sure it's always closed, no matter any exceptions occurring while opening and using the fileWiter.
Maybe your source code is wrongly encoded? How is your editor or your IDE set?
I tried to write the same code but print data into txt file and Arabic letters appears normal but this issue happened in csv
What program did you use to open the csv file?
Microsoft excel 365
|
1

When Microsoft Excel saves files at CSV UTF-8 it adds three bytes to the beginning of the file. Here is a java function to accomplish this purpose:

  public static void makeFileExcelUTF8(String filename) {
    try {
      Path path = Paths.get(filename);
      byte[] fileBytes = Files.readAllBytes(path);
      if (fileBytes[0] == (byte) 239 && fileBytes[1] == (byte) 187 && fileBytes[2] == (byte) 191) {
        System.out.println(filename + " is already in excel utf8 format");
        return;
      }
      byte[] prefixBytes = new byte[3];
      prefixBytes[0] = (byte) 239;
      prefixBytes[1] = (byte) 187;
      prefixBytes[2] = (byte) 191;
      Files.write(path, prefixBytes);
      Files.write(path, fileBytes, StandardOpenOption.APPEND);
    }
    catch (Exception e) {
      System.out.println("problem saving into excel utf8 format");
    }
  }

Comments

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"?

1 Comment

Ask this as a separate question.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.