Reputation: 45
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 ?
Upvotes: 1
Views: 8321
Reputation: 11
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");
}
}
Upvotes: 0
Reputation: 4162
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.
Upvotes: 0
Reputation: 204
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"?
Upvotes: 0
Reputation: 2493
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");
Upvotes: 3